Parse.Promise在创建后没有运行

时间:2014-07-31 07:31:00

标签: javascript parse-platform

基本上我在这里尝试做的是在matchCenterComparisonPromise大于0的情况下创建eBayResults.length作为Parse的承诺。console.log('amil eh');已注销,所以我知道它确实大于0。

然而,matchCenterComparisonPromise似乎无法运行,尽管事后我会调用return matchCenterComparisonPromise;。我没有把回程电话放在正确的地方,还是我做了一个不同的错误导致它不能运行?

function matchCenterComparison(eBayResults) {   

  console.log('eBayResults are the following:' + eBayResults);

  if (eBayResults.length > 0) {
      console.log('amil eh');

    var matchCenterComparisonPromise = new Parse.Promise(function(){
    console.log('yes the ebay results be longer than 0');

    //Query users MComparisonArray with these criteria  
    var mComparisonArray = Parse.Object.extend("MComparisonArray");
    var mComparisonQuery = new Parse.Query(mComparisonArray);
    mComparisonQuery.contains('Name', 'MatchCenter');
    //mComparisonQuery.contains("MCItems", eBayResults);

    console.log('setup query criteria, about to run it');

    mComparisonQuery.find({
      success: function(results) {
      console.log('MatchCenter comparison results :' + results);
      // No new items                      
      if (results.length > 0) {
        console.log("No new items, you're good to go!");
      }
      // New items found
      else if (results.length === 0) {
        console.log('no matching mComparisonArray, lets push some new shit');
        //replace MCItems array with contents of eBayResults
        Parse.Object.destroyAll(mComparisonArray);

        var newMComparisonArray = new mComparisonArray();
        newMComparisonArray.set('Name', 'MatchCenter');
        newMComparisonArray.set('MCItems', eBayResults);
        //newMComparisonArray.set("parent", Parse.User());

        console.log('yala han save il hagat');
        // Save updated MComparisonArray  
        newMComparisonArray.save().then({
        success: function() {
          console.log('MComparisonArray successfully created!');
          //status.success('MComparisonArray successfully created!');
        },
        error: function() {
          console.log('nah no MComparisonArray saving for you bro:' + error);
          //status.error('Request failed');
        }
        });
        //send push notification

      }
      console.log('MatchCenter Comparison Success!');
      },

      error: function(error) {
      console.log('nah no results for you bro:' + error);
      }
    });
    });

      return matchCenterComparisonPromise;

  }       
}

1 个答案:

答案 0 :(得分:1)

我不确定你能否构建这样的承诺(至少我从来没有见过这样的承诺)。

我所做的是创建一个承诺,然后从其他代码中引用它:

var myPromise = new Parse.Promise();

if (someExpression) {
    // do some work, possibly async
    myPromise.resolve(output);
} else {
    myPromise.reject({ message: 'No work done, expression failed' });
}
return myPromise;

这是基本模式,根据需要进行扩展。