所以我的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++ )
当我在Cloud Code中运行此代码时,它会返回以下错误:
'TypeError: Cannot read property 'length' of undefined at main.js:43:60'
我做错了什么?谢谢
答案 0 :(得分:0)
.length未定义,因为request.params.push是一个对象。看起来你想要使用输入参数request.params.push来迭代你传入这个云函数的列表,如果/假设调用者正在传入一个有效的JSON,那就是&#39; push&#39;然后你可以做这样的事情
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
});