2016-03-04 37 views
0

遊戲結構: Parse.com雲代碼中找到相關的內容

 
    objectId - String 
    name - String 
    genres - Array 
遊戲數據:
 
    ObjectId  name genres 
    ===================================== 
    gawtttBGc2 AAA  ["a", "d"] 
    gawtttBGc9 BBB  ["b", "p"] 
    gawtutrGc4 CCC  ["a", "b", "d"] 
    gawttuowc7 EEE  ["d"] 
雲代碼:
 
     var gQuery = new Parse.Query(Games); 
     var foundGame; 
     var relatedGames; 
     gQuery.equalTo('name', req.params.name).first({ 
      success: function(game) { 
       foundGame = game; 
      }, 
      error: function(error) { 
       return []; 
      } 
     }).then(function() { 
      res.render('games/show', { 
       game: foundGame, 
       relatedGames: relatedGames 
      }); 
     }, 
     function() { 
      res.send(500, 'Failed finding'); 
     }); 
我如何去獲得基於流派在relatedGames變量來存儲相關的遊戲嗎?

例如:

 
    if req.params.name = AAA then 
     relatedGames = CCC, EEE 
    if req.params.name = BBB then 
     relatedGames = CCC 
    if req.params.name = CCC then 
     relatedGames = AAA, BBB 
使用 「containedin」的在equalTo的「成功」的功能,如下圖所示,但它沒有工作,我已經試過。
 
     var gQuery = new Parse.Query(Games); 
     var foundGame; 
     var relatedGames; 
     gQuery.equalTo('name', req.params.name).first({ 
      success: function(game) { 
       foundGame = game; 
       var newQuery = new Parse.Query(Games); 
       newQuery.containedIn('genres', foundGame.get('genres')).find({ 
        success: function(results){ 
         relatedGames = results; 
        }, 
        error: function(error){ 
         console.log(error); 
        } 
       }); 
      }, 
      error: function(error) { 
       return []; 
      } 
     }).then(function() { 
      res.render('games/show', { 
       game: foundGame, 
       relatedGames: relatedGames 
      }); 
     }, 
     function() { 
      res.send(500, 'Failed finding'); 
     }); 

回答

0

玩過「承諾」,並能夠得到它的工作。

 
     var gQuery = new Parse.Query(Games); 
     var foundGame; 
     var relatedGames; 
     gQuery.equalTo('nameShort', req.params.name).find().then(function(games) { 
      foundGame = games[0]; 
      var promise = Parse.Promise.as(); 
      promise = promise.then(function(){ 
       return gQuery.containedIn('genres', foundGame.get('genres')).find().then(function(results) { 
        relatedGames = results; 
        return Parse.Promise.as('Find Related Games'); 
       }); 
      }); 
      return promise; 
     }).then(function() { 
      res.render('games/show', { 
       game: foundGame, 
       relatedGames: relatedGames 
      }); 
     }, 
     function() { 
      res.send(500, 'Failed finding'); 
     }); 

仍在試圖瞭解更多「無極」工作完全,但希望這是如何幫助別人。

相關問題