gpt4 book ai didi

javascript - Parse.com Cloud 代码 - 在保存之前创建另一个对象的新实例

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:27 28 4
gpt4 key购买 nike

我有两个类(class),小组和成员(member)资格。当一个小组建立后,我希望也能建立其相应的成员资格。我尝试这样做:

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 triggered for Group for user xgzDnZ9h0A Input: {"original":null,"update":{"admin":{"objectId":"xgzDnZ9h0A","className":"_User","__type":"Pointer"},"name":"test","public":false}} Result: undefined

不太确定错误是什么。我怎样才能调试这样的代码以便我可以轻松地隔离问题?

最佳答案

让 beforeSave 仅处理验证,并将您的成员(member)代码移至 afterSave。问题可能是您正在尝试存储与尚未保存的对象的关系(因为您在 beforeSave 中,该对象还没有 objectId)

关于javascript - Parse.com Cloud 代码 - 在保存之前创建另一个对象的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505207/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com