我正在寻找一种使用云代码自动更新数据的方法。使用Parse.com中的云代码自动更新数据
让我说我有一个类Table
。我有三列:firstname
,lastname
和fullname
。
目前,我只有firstname
和lastname
数据。列fullname
仍为空。
是否可以自动填入fullname
,只需要合并firstname
和lastname
中的值?
谢谢
我正在寻找一种使用云代码自动更新数据的方法。使用Parse.com中的云代码自动更新数据
让我说我有一个类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钩子来维护添加的数据。
是的,你可以只是把一个值“全名”时要保存“名字”和“姓氏”(cloudcode之前)。或者,你可以使用之前保存/后保存cloudcode函数值插入该列:
看看:
https://parse.com/docs/cloud_code_guide#webhooks-beforeSave https://parse.com/docs/cloud_code_guide#webhooks-afterSave
谢谢你的回答。我想我忘了提及,我已经得到了我的数据。所以,我不想做“保存数据”。根据我的理解,每次执行“保存数据”时都会执行“afterSave”或“beforeSafe”。有没有其他的方法呢? – Gibran 2015-03-30 22:29:28
感谢您的帮助。我试过你的代码,但是我得到了这个错误信息:'{“code”:141,“error”:“TypeError:无法在main.js:11:40 \ n处调用未定义的每个' e(Parse.js:2:8151)\ n在Parse.js:2:7600 \ n在Array.forEach(本地)\ n在Object.x.each.x.forEach [as _arrayEach](Parse.js: (Parse.js:2:7551)\ n at null。\ u003canonymous \ u003e(Parse.js:2:8230)\ n at e(Parse.js:2: 8151)\ n在Parse.js:2:8904 \ n在克(Parse.js:2:8641)“}' – Gibran 2015-04-01 13:25:32
请参阅编辑。确保要求underscorejs,这是非常有用的。或者用native for循环替换它们。 – danh 2015-04-01 13:31:57