使用 Parse.Could.AfterSave 将关系添加到 ParseObject
using Parse.Could.AfterSave on adding a relation to a ParseObject
我只是在体验 parse sdk,我想在客户端添加两个表之间的关系时使用云代码来增加数字列(例如用户喜欢 post),在解析文档中只是基于保存对象递增列的代码,而不是使用 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();
}
});
经过一番努力解析文档!最后我用这种方式解决了我的问题,它可以很好地计算特定对象的两个表之间的关系,希望这可以节省其他人一些时间:
Parse.Cloud.afterSave("SavedObject", function(request) {
var likeQuery = request.object.relation("likers").query();
likeQuery.count()(function(likersCount){
request.object.set("likersCountColumn",likersCount);
console.log("Likers Count Is : "+ likersCount);
request.object.save();
});
});
我只是在体验 parse sdk,我想在客户端添加两个表之间的关系时使用云代码来增加数字列(例如用户喜欢 post),在解析文档中只是基于保存对象递增列的代码,而不是使用 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();
}
});
经过一番努力解析文档!最后我用这种方式解决了我的问题,它可以很好地计算特定对象的两个表之间的关系,希望这可以节省其他人一些时间:
Parse.Cloud.afterSave("SavedObject", function(request) {
var likeQuery = request.object.relation("likers").query();
likeQuery.count()(function(likersCount){
request.object.set("likersCountColumn",likersCount);
console.log("Likers Count Is : "+ likersCount);
request.object.save();
});
});