所以我的 Parse Core 中有一个大约 200 行的列表。我正在尝试创建一个运行整个列表的作业,并将 push
的整个列更改为 0
。我试图用这段代码来做到这一点:
Parse.Cloud.job("SetPush", function(request, response) {
//take in JSON with dict
var newts = new Array();
for ( var i = 0; i < request.params.push.length; i++ )
{
//add these entries to db
var DataClass = Parse.Object.extend("AllTeams");
var dataupdate = new DataClass();
var origdata = request.params.datalist[i];
dataupdate.set("push", "0");
newts[i]=dataupdate; //add another item to list
}
Parse.Object.saveAll(newts,{
success: function(list) {
// All the objects were saved.
response.success("ok " ); //saveAll is now finished and we can properly exit with confidence :-)
},
error: function(error) {
// An error occurred while saving one of the objects.
response.error("failure on saving list ");
},
});
//default body does not do response.success or response.error
});
如您所见,我的类是 SetPush
,我想一直向下更新 push
列。我认为问题在于:
for ( var i = 0; i < request.params.push.length; i++ )
当我在云代码中运行此代码时,它返回此错误:
'TypeError: Cannot read property 'length' of undefined at main.js:43:60'
我做错了什么?谢谢
最佳答案
.length 未定义,因为 request.params.push 是一个对象。看起来您想使用输入参数 request.params.push 迭代传入此云函数的列表,如果/假设调用者传入有效的 JSON 作为“push”,那么您可以执行类似的操作
Parse.Cloud.job("SetPush", function(request, response) {
//take in JSON with dict
var parsedJson = JSON.parse( request.params.push );
var newts = new Array();
for ( var i = 0; i < parsedJson.length; i++ )
{
//add these entries to db
var DataClass = Parse.Object.extend("AllTeams");
var dataupdate = new DataClass();
var origdata = request.params.datalist[i];
dataupdate.set("push", "0");
newts[i]=dataupdate; //add another item to list
}
Parse.Object.saveAll(newts,{
success: function(list) {
// All the objects were saved.
response.success("ok " );
//saveAll is now finished and we can properly exit with confidence :-)
},
error: function(error) {
// An error occurred while saving one of the objects.
response.error("failure on saving list ");
},
}); //default body does not do response.success or response.error
});
关于javascript - 解析云代码 全部保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375854/