更新 Cloud Code 中的现有 Parse 对象 [英] Updating existing Parse object in Cloud Code

查看:47
本文介绍了更新 Cloud Code 中的现有 Parse 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在后端使用 Parse,想搜索现有的好友请求并更新它们,而不是创建新的请求(如果已经存在).

I am using Parse for my backend and want to search for existing friend requests and update those instead of creating new ones (if there is already an existing one).

我以为我知道该怎么做,但是当我提交新的好友请求时,它们会被创建为新对象,而不是更新旧对象,即使我找到了现有请求.

I thought I figured out how to do it but when I submit new friend requests they get created as new objects instead of updating the old one, even though I found an existing request.

这是我使用的代码:

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.任何想法或想法表示赞赏!

There isn't a lot going on here. The query does come back successful with the correct entry in the database. I can confirm that I get the correct objectId for the existing item in the database. Any thoughts or ideas are appreciated!

推荐答案

您无法手动设置对象的 objectId.

You can't manually set the objectId of an object.

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

If you want beforeSave to NOT create a new object (which is what you're about to do when beforeSave is called), you need to manually update the existing object and then respond with a failure. If you respond with response.success(), the object will be saved normally.

在您的代码中,您似乎没有对现有对象进行任何更改.您真正需要做的就是返回 response.error (https://parse.com/docs/cloud_code_guide#functions-onsave)

In your code, you don't seem to make any changes to the existing object. All you really need to do is to return response.error (https://parse.com/docs/cloud_code_guide#functions-onsave)

当然,你也应该以某种方式在你的代码中处理这个问题.通过警告用户或静默处理它.

Of course, you should also handle this in your code somehow. Either by alerting the user, or handling it silently.

然而;如果新的好友请求已经存在,为什么您的代码会尝试保存它?您的应用应该知道存在一个并禁用好友请求按钮或 UI 提供的任何内容.

However; why does your code attempt to save a new friend request if one already exist? Your app should know that one exists and disable the friend request button or whatever the UI offers.

这篇关于更新 Cloud Code 中的现有 Parse 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆