简体   繁体   English

Parse.com背景作业云代码无法调用未定义的方法'then'

[英]Parse.com Background Job cloud code cannot call method 'then' of undefined

I am new to cloudcode and javascript so promise concepts are still not clear, I am trying to upload read a level file and saving it to the Class Levels using background job but in below code I am getting error Cannot call method 'then' of undefined 我是cloudcode和javascript的新手,所以承诺概念仍然不清楚,我正在尝试上载读取的级别文件,并使用后台作业将其保存到Class Levels,但是在下面的代码中,我得到错误无法调用未定义的方法'then'

I am not sure whats going wrong ? 我不确定出什么问题了吗? and it is very difficult to debug when your concepts are not clear s it should be, so it would be great help if some one can clear my concepts of Parse promises and also help with the above error in this code. 当您的概念不清楚时,调试起来非常困难,因此,如果有人可以清除我的Parse Promise概念,并且对代码中的上述错误有所帮助,这将对您大有帮助。

Parse.Cloud.job("uploadLevelJob" , function(request , status){
    var Levels= Parse.Object.extend("Levels");
    var levels= request.params.arrayOfLevels;
    var promise = new Parse.Promise.as();
    _.each(levels , function(level){
        promise = promise.then(function() {
            var tempObj = new Levels();
            tempObj.set("levelId",parseInt(level[i][0]));
            tempObj.set("levelName",level[i][1]);
            return result.save();          
        });
        return promise;
    }).then(function(){
        status.success("Uploaded ! ");  
    });

});

It seems you are trying to use promises in series, but you can actually use parallel promises in your case. 似乎您正在尝试串联使用Promise,但实际上您可以在情况下使用并行Promise Because each save is independent from each other. 因为每次保存都相互独立。 By the way i and result seem to be undefined in your code. 顺便说一句, iresult似乎在您的代码中未定义。 I assume the levels is an array like [[levelId, "levelName"], ...] 我假设levels是一个像[[levelId, "levelName"], ...]的数组

var Levels = Parse.Object.extend("Levels");

Parse.Cloud.job("uploadLevelJob" , function(request , status){
  var levels = request.params.arrayOfLevels;

  var promises = [];
  _.each(levels , function(level) {
    var tempObj = new Levels();
    tempObj.set("levelId",parseInt(level[0]));
    tempObj.set("levelName",level[1]);
    promises.push(tempObj.save());
  });

  return Parse.Promise.when(promises).then(function() {
    status.success("Migration completed successfully.");
  }, function(error) {
    status.error("Uh oh, something went wrong.");
  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM