使用 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屋!