Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parse platform 更新云代码中的现有解析对象_Parse Platform - Fatal编程技术网

Parse platform 更新云代码中的现有解析对象

Parse platform 更新云代码中的现有解析对象,parse-platform,Parse Platform,我正在为我的后端使用Parse,希望搜索现有的好友请求并更新这些请求,而不是创建新的请求(如果已经存在) 我想我知道怎么做了,但当我提交新的好友请求时,它们会被创建为新对象,而不是更新旧对象,即使我找到了一个现有的请求 以下是我正在使用的代码: Parse.Cloud.beforeSave("FriendRequest", function(request, response) { //search for an existing friend request with the sam

我正在为我的后端使用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。任何想法或想法都将受到赞赏

无法手动设置对象的
objectId

如果希望
beforeSave
不创建新对象(这是调用
beforeSave
时要执行的操作),则需要手动更新现有对象,然后以失败响应。如果使用
response.success()
进行响应,对象将正常保存

在代码中,似乎没有对现有对象进行任何更改。您真正需要做的就是返回
响应。error
()

当然,您也应该在代码中以某种方式处理此问题。要么提醒用户,要么静默处理


但是,;如果一个新的好友请求已经存在,为什么代码会尝试保存它?你的应用程序应该知道有一个存在,并禁用好友请求按钮或UI提供的任何功能。

Hi!谢谢你的评论。嗯,我是在一篇一年多前的帖子中,从解析论坛上得到了设置objectId的想法,但这是他们的一名员工提出的,所以我认为这会奏效。我发布的代码已经删除了很多东西,因为我试图找出它不起作用的原因,所以我想删除不必要的东西。我真正想做的是更新请求的状态(例如,接受或拒绝)。因此,发送的请求实际上与我查询的请求不同。嗯,可能是我在手动设置objectId部分出错了。你有那个帖子的链接吗?哦,看起来你是对的。我从这篇文章中得到了这个想法:但是他们试图设置objectId的评论不是来自Parse员工。