我正在寻找一种使用云代码自动更新数据的方法。
假设我有一堂课Table
。它里面,我有三个栏:firstname
,lastname
,和fullname
。
目前,我只有firstname
和lastname
数据。列fullname
仍为空。
是否有可能填补了fullname
自动,仅通过在组合的价值firstname
和lastname
?
谢谢,
@RoyH 100%有权在创建新对象时维护您的计算列。要进行初始迁移,请尝试使用云功能,例如:
var _ = require("underscore");
Parse.Cloud.define("addFullnames", function(request, response) {
// useMasterKey if the calling user doesn't have permissions read or write to Table
Parse.Cloud.useMasterKey();
var query = new Parse.Query("Table");
// we'll call tables > 1000 an 'advanced topic'
query.limit = 1000;
query.find().then(function(results) {
_.each(results, function(result) {
var firstname = result.get("firstname") || "";
var lastname = result.get("lastname") || "";
result.set("fullname", (firstname + " " + lastname).trim());
});
return Parse.Object.saveAll(results);
}).then(function(results) {
response.success(results);
}, function(error) {
response.error(error);
});
});
这样称呼它:
curl -X POST \
-H "X-Parse-Application-Id: your_app_id_here" \
-H "X-Parse-REST-API-Key: your_rest_key_here" \
-H "Content-Type: application/json" \
https://api.parse.com/1/functions/addFullnames
您可以分解_.each()中的代码以创建在此处调用的函数,并使用beforeSave挂钩来维护添加的数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句