游戏结构:
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我尝试在equalTo的“success”函数中使用“containedIn”,如下所示,但它不起作用。
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 :(得分: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'); });
仍在努力了解" Promise" 的确如何运作,但希望这有助于其他人。