访问Parse.com Cloud Code之前的原始字段保存功能 [英] Accessing original field in Parse.com Cloud Code beforeSave function

查看:82
本文介绍了访问Parse.com Cloud Code之前的原始字段保存功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终目标是使用Cloud Code中的 beforeSave 功能检测现有Parse对象与传入更新之间的更改。

The ultimate goal is to detect changes between an existing Parse object and the incoming update using the beforeSave function in Cloud Code.

从parse.com提供的Cloud Code日志中,可以看到输入 beforeSave 包含一个名为 original 和另一个名为 update

From the Cloud Code log available through parse.com, one can see the input to beforeSave contains a field called original and another one called update.

Cloud Code log:

Cloud Code log:

Input: {"original": { ... }, "update":{...}

我想知道我们是否以及如何访问原始字段以便在保存之前检测更改字段。

I wonder if, and how, we can access the original field in order to detect changing fields before saving.

请注意,我已经尝试了几种方法来解决这个问题但没有成功:

Note that I've already tried several approaches for solving this without success:


  • using(object).changedAttributes()

  • using(object).previousAttributes()

  • 在使用新数据更新之前获取现有对象

注意 request.object.changedAttributes()
在beforeSave和afterSave中使用时返回 false - 有关更多详细信息,请参见下文:

Note on request.object.changedAttributes(): returns false when using in beforeSave and afterSave -- see below for more details:

记录 before_save - 为了便于阅读而进行了总结:

Log for before_save -- summarised for readability:

Input: { original: {units: '10'}, update: {units: '11'} }
Result: Update changed to { units: '11' }
[timestamp] false <--- console.log(request.object.changedAttributes())

记录相应的 after_save

[timestamp] false <--- console.log(request.object.changedAttributes())


推荐答案

<$ c> <$ c> $ C> changedAttributes()。它似乎总是回答错误 - 或至少在之前保存,在那里合理地需要它。 (参见此处,以及其他类似的帖子)

There is a problem with changedAttributes(). It seems to answer false all the time -- or at least in beforeSave, where it would reasonably be needed. (See here, as well as other similar posts)

这是一个通用的解决方案来做changeAttributes应该做的事情。

Here's a general purpose work-around to do what changedAttributes ought to do.

// use underscore for _.map() since its great to have underscore anyway
// or use JS map if you prefer...

var _ = require('underscore');

function changesOn(object, klass) {
    var query = new Parse.Query(klass);
    return query.get(object.id).then(function(savedObject) {
        return _.map(object.dirtyKeys(), function(key) {
            return { oldValue: savedObject.get(key), newValue: object.get(key) }
        });
    });
}

// my mre beforeSave looks like this
Parse.Cloud.beforeSave("Dummy", function(request, response) {
    var object = request.object;
    var changedAttributes = object.changedAttributes();
    console.log("changed attributes = " + JSON.stringify(changedAttributes));  // null indeed!

    changesOn(object, "Dummy").then(function(changes) {
        console.log("DIY changed attributes = " + JSON.stringify(changes));
        response.success();
    }, function(error) {
        response.error(error);
    });
});

当我更改 someAttribute 时(一个数字列)在 Dummy 实例上)从32到1222通过客户端代码或数据浏览器,日志显示:

When I change someAttribute (a number column on a Dummy instance) from 32 to 1222 via client code or data browser, the log shows this:


I2015-06-30T20:22:39.886Z]改变了属性=假

I2015-06-30T20:22:39.886Z]changed attributes = false

I2015-06-30T20:22:39.988Z] DIY改变了属性=
[{oldValue:32,newValue:1222}]

I2015-06-30T20:22:39.988Z]DIY changed attributes = [{"oldValue":32,"newValue":1222}]

这篇关于访问Parse.com Cloud Code之前的原始字段保存功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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