访问 Parse.com Cloud Code beforeSave 函数中的原始字段
Accessing original field in Parse.com Cloud Code beforeSave function
最终目标是使用 Cloud Code 中的 beforeSave
函数检测现有 Parse 对象和传入更新之间的变化。
从 parse.com 提供的 Cloud Code 日志中,可以看到 beforeSave
的输入包含一个名为 original
的字段和另一个名为 update
的字段。
云码日志:
Input: {"original": { ... }, "update":{...}
我想知道我们是否以及如何访问原始字段以便在保存之前检测更改的字段。
请注意,我已经尝试了多种方法来解决此问题但均未成功:
- 使用 (object).changedAttributes()
- 使用 (object).previousAttributes()
- 获取现有对象,然后用新数据更新它
关于 request.object.changedAttributes()
的注释:
returns false
在 beforeSave 和 afterSave 中使用时——更多细节见下文:
记录 before_save
-- 为便于阅读而总结:
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())
changedAttributes()
有问题。它似乎总是回答错误——或者至少在 beforeSave
中,在合理需要它的地方。 (参见 here,以及其他类似的帖子)
这是一个通用的解决方法,可以完成 changedAttributes 应该做的事情。
// 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 时,日志显示如下:
I2015-06-30T20:22:39.886Z]changed attributes = false
I2015-06-30T20:22:39.988Z]DIY changed attributes =
[{"oldValue":32,"newValue":1222}]
最终目标是使用 Cloud Code 中的 beforeSave
函数检测现有 Parse 对象和传入更新之间的变化。
从 parse.com 提供的 Cloud Code 日志中,可以看到 beforeSave
的输入包含一个名为 original
的字段和另一个名为 update
的字段。
云码日志:
Input: {"original": { ... }, "update":{...}
我想知道我们是否以及如何访问原始字段以便在保存之前检测更改的字段。
请注意,我已经尝试了多种方法来解决此问题但均未成功:
- 使用 (object).changedAttributes()
- 使用 (object).previousAttributes()
- 获取现有对象,然后用新数据更新它
关于 request.object.changedAttributes()
的注释:
returns false
在 beforeSave 和 afterSave 中使用时——更多细节见下文:
记录 before_save
-- 为便于阅读而总结:
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())
changedAttributes()
有问题。它似乎总是回答错误——或者至少在 beforeSave
中,在合理需要它的地方。 (参见 here,以及其他类似的帖子)
这是一个通用的解决方法,可以完成 changedAttributes 应该做的事情。
// 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 时,日志显示如下:
I2015-06-30T20:22:39.886Z]changed attributes = false
I2015-06-30T20:22:39.988Z]DIY changed attributes = [{"oldValue":32,"newValue":1222}]