我有两个班级,团体和会员资格。制作小组时,我也希望制作相应的会员资格。我尝试这样做:
Parse.Cloud.beforeSave("Group", function(request, response) {
var name = request.object.get("name");
if (!name) {
response.error("A Group must have a name");
} else {
if (!(/^\w+$/i.test(name))){
response.error("Only letters and numbers, please.");
}
var query = new Parse.Query("Group");
query.equalTo("name", name);
query.first({
success: function(object) {
if (object) {
response.error("A group with this name exists.");
} else {
createMembershipForGroup(request.object, Parse.User);
response.success();
}
},
error: function(error) {
response.error("Could not validate Uniqueness");
}
});
}
});
createMembershipForGroup = function(group, user) {
var MembershipClass = Parse.Object.extend("Membership");
var membership = new MembershipClass();
membership.set("group", group);
membership.set("user", user);
membership.save(null,{
success:function(membership) {
response.success(membership);
},
error:function(error) {
response.error(error);
}
});
}
我收到错误,说出以下内容:
E2014-05-06T21:40:10.740Z] v30:为群组触发了before_save user xgzDnZ9h0A输入: {"原":空,"更新" {"管理" {"的ObjectID":" xgzDnZ9h0A&#34 ;, "的className":" _user"" __类型":"指针"}"名称":"测试""公共":假}} 结果:未定义
不太清楚错误是什么。我怎样才能调试这样的代码,以便我可以轻松地隔离问题?
答案 0 :(得分:1)
让beforeSave仅处理验证,并将您的会员代码移至afterSave。问题可能是你试图存储一个尚未保存的对象的关系(因为你在beforeSave中,该对象还没有objectId)
答案 1 :(得分:0)
问题是你试图调用异步函数并在等待完成之前离开。
createMembershipForGroup(request.object, Parse.User);//Calling async func
response.success();//Leaves beforeSave function
您应该在查询的成功响应中编写createMembershipForGroup函数,并在此处返回您的response.success()方法
membership.save(null,{
success:function(membership) {
response.success(membership); // this should be response of beforeSave
},
error:function(error) {
response.error(error);
}
});
如果没有帮助,我会发布工作代码。