【发布时间】:2014-04-09 16:39:17
【问题描述】:
我正在使用 Parse 作为我的后端,并希望搜索现有的好友请求并更新这些请求,而不是创建新请求(如果已经存在的话)。
我以为我知道该怎么做,但是当我提交新朋友请求时,即使我找到了现有请求,它们也会被创建为新对象而不是更新旧对象。
这是我正在使用的代码:
Parse.Cloud.beforeSave("FriendRequest", function(request, response) {
//search for an existing friend request with the same "from" and "to"
var query = new Parse.Query("FriendRequest");
query.equalTo("from", request.object.get("from"))
.equalTo("to", request.object.get("to"));
query.find({
success: function(results) {
if(results.length > 0)
{
var result = results[0];
//the new request id is undefined as expected
console.log("request id: " + request.object.id);
//the result id is valid for an object in the db as expected
console.log("result id: " + results[0].id);
//set the id of the request to the id of the existing db object
request.object.id = results[0].id;
//the valid id is now in the request object id
console.log("request id: " + request.object.id);
//after response.success, the database shows a new entry
//with a different id
//instead of updating the existing entry
response.success();
}
}
});
});
这里没有很多事情发生。查询确实返回成功,并在数据库中输入了正确的条目。我可以确认我获得了数据库中现有项目的正确 objectId。任何想法或想法表示赞赏!
【问题讨论】:
标签: parse-platform