使用Parse.com中的云代码自动更新数据 [英] automatically updating data using cloud code in Parse.com
问题描述
我正在寻找一种使用云代码自动更新数据的方法.
I'm looking a method to automatically updating data using cloud code.
让我说一堂课Table
.
在其中,我有三列:firstname
,lastname
和fullname
.
Let say I have a class Table
.
inside of it, I have three column : firstname
, lastname
, and fullname
.
当前,我仅具有firstname
和lastname
数据.列fullname
仍然为空.
Currently, I only have firstname
and lastname
data only. Column fullname
is still empty.
是否可以通过仅组合firstname
和lastname
中的值来自动填充fullname
?
Is it possible to fill the fullname
automatically, by just combining the value in firstname
and lastname
?
谢谢
推荐答案
@RoyH在创建新对象时维护您的计算列的100%权利.要进行初始迁移,请尝试使用云功能,例如:
@RoyH is 100% right to maintain your computed column as new objects are created. To do an initial migration, try a cloud function, like:
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挂钩来维护添加的数据.
You can factor out the code inside _.each() to make a function that's called here and by a beforeSave hook to maintain the data as it is added.
这篇关于使用Parse.com中的云代码自动更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!