Parse.com云码查找相关内容
Parse.com cloud code find related content
游戏结构:
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');
});
玩了 "Promise" 并且能够让它工作。
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" 的工作原理,但希望这对其他人有所帮助。
游戏结构:
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'); });
玩了 "Promise" 并且能够让它工作。
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" 的工作原理,但希望这对其他人有所帮助。