2014-05-11 68 views
0

我試圖在解析上運行一些雲功能,但originalFavorited.first未運行。解析依賴對方的多個查詢的雲代碼

var _ = require('underscore'); 
Parse.Cloud.define("userFeed", function (request, response) { 
    if (request.user == undefined) { 
     response.error("User must be logged in!"); 
    } 

    var friendsQuery = new Parse.Query("Friend"); 
    friendsQuery.equalTo("fromUser", request.user); 

    var badPhotosQuery = new Parse.Query("Photo"); 
    badPhotosQuery.greaterThanOrEqualTo("flagCount", 3); 

    var userFlaggedQuery = new Parse.Query("Photo"); 
    userFlaggedQuery.containedIn("flagCount", request.user.flaggedItems); 

    var allBadPhotosQuery = new Parse.Query.or(badPhotosQuery, userFlaggedQuery); 

    var activitiesQuery = new Parse.Query("Activity"); 
    activitiesQuery.skip(request.params.startRecord); 
    activitiesQuery.limit(request.params.numberRecords); 
    activitiesQuery.equalTo("type", "favorite"); 
    activitiesQuery.matchesKeyInQuery("fromUser", "toUser", friendsQuery); 
    activitiesQuery.descending("createdAt"); 
    activitiesQuery.include("photo") 
    activitiesQuery.include("fromUser"); 
    activitiesQuery.notEqualTo("fromUser", request.user); 
    activitiesQuery.doesNotMatchQuery("photo", allBadPhotosQuery); 


    var parsedResults = []; 
    activitiesQuery.find().then(function (results) { 
     return results; 
    }).then(function (results) { 

     var promise = Parse.Promise.as(); 

     _.each(results, function (activity) { 
      promise = promise.then(function() { 
       console.log("Parsing results for " + activity); 
       var originalFavorited = new Parse.Query("Activity"); 
       originalFavorited.ascending("createdAt"); 
       console.log(activity.get("photo")); 
       originalFavorited.equalTo("photo", activity.get("photo")); 
       originalFavorited.include("photo"); 
       originalFavorited.include("fromUser"); 
       originalFavorited.equalTo("type", "favorite"); 

       originalFavorited.first(function (origResult) { 

        console.log("Found original"); 
        parsedResults.push(origResult); 
        return parsedResults; 
       }); 
      }); 
     }); 
     return promise; 
    }).then(function() { 
     var filteredResults = _.uniq(parsedResults, false); 
     response.success(filteredResults); 
    }); 

對於我的輸出console.log (activity.get("photo"));正在輸出對象。 photo是我的Photo類的指針。

我是否正確地做了承諾,以便代碼正在同步運行?

回答

0

試試這個:

var parsedResults = []; 
activitiesQuery.find().then(function (results) { 

    /// ---- REMOVED HERE ---- 
    // return results; 
    // }).then(function (results) { 

    var promise = Parse.Promise.as(); 

    _.each(results, function (activity) { 
     promise = promise.then(function() { 
      console.log("Parsing results for " + activity); 
      var originalFavorited = new Parse.Query("Activity"); 
      originalFavorited.ascending("createdAt"); 
      console.log(activity.get("photo")); 
      originalFavorited.equalTo("photo", activity.get("photo")); 
      // the two line belows can be ignored since you have the same value in activity.get("photo") and activity.get("fromUser"), included previously 
      originalFavorited.include("photo"); 
      originalFavorited.include("fromUser"); 
      originalFavorited.equalTo("type", "favorite"); 

      /// ---- ADDED LINE BELOW ---- 
      originalFavorited.limit(1); 
      originalFavorited.find(function (subResults) { 

       console.log("Found original"); 

       /// ADDED CHECK HERE 
       if (subResults.length > 0){ 
        parsedResults.push(subResults[0]); 
       } 

       /// ---- CHANGED HERE ---- 
       var subPromise = Parse.Promise.as(); 
       subPromise.resolve(parsedResults); 
       return subPromise ; // returning a promise to promise series 

      /// ---- ADDED ERROR HANDLING HERE ---- 
      },function(error){ 

       console.log("Unable to retrieve first element in originalFavorited query. Cause: " + error.message); 
       var errPromise = Parse.Promise.as(); 
       errPromise.reject(error); 
       return errPromise; // return failed promise to promise series just for continue the operation series up to the end 
      }); 
     }); 
    }); 

    return promise; 

},function(error){ /// ---- ADDED ERROR HANDLING HERE ---- 

    console.log("Unable to perform find on activitiesQuery : " + error.message); 
    response.error(error.message); 

}).then(function() { 
    var filteredResults = _.uniq(parsedResults, false); 
    response.success(filteredResults); 
}); 

注意:不要忘了自定義功能解析上限的執行,所以選擇查詢的適當量爲每個自定義函數調用執行

+0

感謝不幸的是,'Found original'查詢似乎沒有被運行。 – Bot

+0

您是否檢查過該查詢是否發生錯誤?嘗試在.first(...)方法中處理錯誤回調。我已經更新了那個答案。 –

+0

錯誤日誌中沒有錯誤,更新代碼 – Bot