我只是在体验 parse sdk 并且我想在客户端添加的两个表之间存在关系时使用云代码来递增数字列(例如用户喜欢帖子),在解析文档中只有用于递增的代码基于保存对象的列,而不是使用 afterSave 的表之间的关系:
Parse.Cloud.afterSave("Comment", function(request) {
query = new Parse.Query("Post");
query.get(request.object.get("post").id, {
success: function(post) {
post.increment("comments");
post.save();
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
});
如何使用 afterSave 来改变两个表之间的关系?任何帮助将不胜感激。
最佳答案
我建议改用 beforeSave。这是我从我的一个项目中提取的示例代码。
Parse.Cloud.beforeSave(ClassConstants.Item.TYPE, function(request, response) {
if(!request.object.isNew()) {
// Retrieve the relationship information in json string format.
// RELATION_QUEUE here is simply a string "queue"
var relQueueJsonStr = JSON.stringify(request.object.op(ClassConstants.Item.RELATION_QUEUE));
if( relQueue !== undefined ) {
var relQueue = JSON.parse(relQueueJsonStr);
// Retrieve the operation being performed to this existing object.
// The value will be "AddRelation" if a the relation "queue" is
// being added to this object
var operation = relQueue.__op;
if (operation == "AddRelation"){
console.log("Relation queue is being added");
// Do what you like here. In your case increment something
response.success();
} else {
// Relation is being removed
console.log("Relation queue is being removed");
response.success();
}
} else {
console.log("No queue relation.");
response.success();
}
} else {
response.success();
}
});
关于android - 使用 Parse.Could.AfterSave 将关系添加到 ParseObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32294748/