如何在保存之前查询 CloudCode 中的对象? [英] How to query objects in the CloudCode beforeSave?

查看:15
本文介绍了如何在保存之前查询 CloudCode 中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 CloudCode beforeSave 函数将 新对象原始 进行比较.我需要将 update 中发送的字段与现有值进行比较.问题是我无法正确获取对象.当我运行查询时,我总是从发送的对象中获取值.

I'm trying to compare a new object with the original using CloudCode beforeSave function. I need to compare a field sent in the update with the existing value. The problem is that I can't fetch the object correctly. When I run the query I always get the value from the sent object.

更新:我尝试了一种不同的方法,可以得到旧的寄存器(已经保存在解析中的那个).但是请求中发送的新请求被旧请求覆盖了.什么?!另一个问题是,即使代码发送了 response.success(),更新也没有保存.我相信我在这里遗漏了一些很明显的东西.或者我遇到了错误或其他问题...

UPDATE: I tried a different approach and could get the old register ( the one already saved in parse). But the new one, sent in the request, was overridden by the old one. WHAT?! Another issue is that, even thought the code sent a response.success(), the update wasn't saved. I believe that I'm missing something pretty obvious here. Or I'm facing a bug or something...

新方法

Parse.Cloud.beforeSave('Tasks', function(request, response) {
  if ( !request.object.isNew() )
  {
    var Task = Parse.Object.extend("Tasks");
    var newTask = request.object;
    var oldTask = new Task();
    oldTask.set("objectId", request.object.id);
    oldTask.fetch()
        .then( function( oldTask )
    {
        console.log(">>>>>> Old Task: " + oldTask.get("name") + " version: " + oldTask.get("version"));
        console.log("<<<<<< New Task: " + newTask.get("name") + " version: " + newTask.get("version"));
        response.success();
    }, function( error ) {
            response.error( error.message );
        }
    );
    }
});

OBJ SENT {"name":"LLL", "version":333}

日志

 I2015-10-02T22:04:07.778Z]v175 before_save triggered for Tasks for user tAQf1nCWuz:
 Input: {"original":{"createdAt":"2015-10-02T17:47:34.143Z","name":"GGG","objectId":"VlJdk34b2A","updatedAt":"2015-10-02T21:57:37.765Z","version":111},"update":{"name":"LLL","version":333}}
 Result: Update changed to {}
 I2015-10-02T22:04:07.969Z]>>>>>> Old Task: GGG version: 111
 I2015-10-02T22:04:07.970Z]<<<<<< New Task: GGG version: 111

注意:我正在通过 cURL 和解析控制台测试登录.

NOTE: I'm testing the login via cURL and in the parse console.

CloudCode beforeSave

Parse.Cloud.beforeSave("Tasks", function( request, response) {
  var query = new Parse.Query("Tasks");
  query.get(request.object.id)
    .then(function (oldObj) {
        console.log("-------- OLD Task: " + oldObj.get("name") + " v: " + oldObj.get("version"));
        console.log("-------- NEW Task: " + request.object.get("name") + " v: " + request.object.get("version"));
    }).then(function () {
        response.success();
    }, function ( error) {
        response.error(error.message);
    }
  );
});

cURL 请求

curl -X PUT 
-H "Content-Type: application/json" 
-H "X-Parse-Application-Id: xxxxx" 
-H "X-Parse-REST-API-Key: xxxxx" 
-H "X-Parse-Session-Token: xxxx" 
-d "{"name":"NEW_VALUE", "version":9999}" 
https://api.parse.com/1/classes/Tasks/VlJdk34b2A

JSON 响应

"updatedAt": "2015-10-02T19:45:47.104Z"

日志日志打印了 originalnew 值,但我也不知道如何访问它.

LOG The log prints the original and the new value, but I don't know how to access it either.

I2015-10-02T19:57:08.603Z]v160 before_save triggered for Tasks for user tAQf1nCWuz:
Input: {"original":{"createdAt":"2015-10-02T17:47:34.143Z","name":"OLD_VALUE","objectId":"VlJdk34b2A","updatedAt":"2015-10-02T19:45:47.104Z","version":0},"update":{"name":"NEW_VALUE","version":9999}}
Result: Update changed to {"name":"NEW_VALUE","version":9999}
I2015-10-02T19:57:08.901Z]-------- OLD Task: NEW_VALUE v: 9999
I2015-10-02T19:57:08.902Z]-------- NEW Task: NEW_VALUE v: 9999

推荐答案

经过大量的测试和错误,我可以弄清楚发生了什么.

After a lot test and error I could figure out what was going on.

事实证明,Parse 正在将具有相同类和 ID 的任何对象合并到一个实例中.这就是为什么我总是在数据库中注册对象或用户发送的对象的原因.老实说,我无法理解这种行为,但无论如何......

Turn out that Parse is merging any objects with the same class and id into one instance. That was the reason why I always had either the object registered in DB or the one sent by the user. I honestly can't make sense of such behavior, but anyway...

Parse javascript sdk 提供了一个名为 Parse.Object.disableSingeInstance 的方法 link 禁用此功能".但是,一旦调用该方法,所有已定义的对象都未定义.这包括发送的对象.Witch 意味着您不能保存发送的对象以供以后参考.

The Parse javascript sdk offers an method called Parse.Object.disableSingeInstance link that disables this "feature". But, once the method is called, all object already defined are undefined. That includes the sent object. Witch means that you can't neither save the sent object for a later reference.

唯一的选择是保存发送的 obj 的键和值,稍后重新创建.因此,我需要在调用 disableSingleInstance 之前捕获请求,将其转换为 JSON,然后禁用单个实例,获取保存在 DB 中的对象并使用保存的 JSON 重新创建发送的对象.

The only option was to save the key and values of the sent obj and recreate it later. So, I needed to capture the request before calling disableSingleInstance, transform it in a JSON, then disable single instance, fetch the object saved in DB and recreate the sent object using the JSON saved.

它不漂亮,绝对不是最有效的代码,但我找不到任何其他方法.如果有人有其他方法,请务必告诉我.

Its not pretty and definitely isn't the most efficient code, but I couldn't find any other way. If someone out there have another approach, by all means tell me.

Parse.Cloud.beforeSave('Tasks', function(request, response) {
  if ( !request.object.isNew() ) {
    var id = request.object.id;
    var jsonReq;
    var Task = Parse.Object.extend("Tasks");
    var newTask = new Task;
    var oldTask = new Task;

    // getting new Obj
    var queryNewTask = new Parse.Query(Task);
    queryNewTask.get(id)
        .then(function (result) {
            newTask = result;

            // Saving values as a JSON to later reference
            jsonReq = result.toJSON();

            // Disable the merge of obj w/same class and id
            // It will also undefine all Parse objects,
            // including the one sent in the request
            Parse.Object.disableSingleInstance();

            // getting object saved in DB
            oldTask.set("objectId", id);
            return oldTask.fetch();
        }).then(function (result) {
            oldTask = result;

            // Recreating new Task sent
            for ( key in jsonReq ) {
                newTask.set( key, jsonReq[key]);
            }

            // Do your job here
        }, function (error) {
            response.error( error.message );
        }
    );
  }
});

这篇关于如何在保存之前查询 CloudCode 中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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