Search code examples
parse-platformparse-cloud-code

Parse.com cloud code find related content


Games Structure:

    objectId - String
    name - String
    genres - Array
    
Games Data:
    ObjectId      name    genres
    =====================================
    gawtttBGc2    AAA     ["a", "d"]
    gawtttBGc9    BBB     ["b", "p"]
    gawtutrGc4    CCC     ["a", "b", "d"]
    gawttuowc7    EEE     ["d"]
    
Cloud Code:
        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');
        });
    
How do I go about getting the related games based on genres to store in relatedGames variable?

ex:

    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
    
I've tried using "containedIn" in "success" function of equalTo as seen below but it didn't work.
        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');
        });
    


Solution

  • Played around "Promise" and was able to get it to work.

            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');
            });
        

    Still trying to understand more how "Promise" works exactly but hope this helps someone else.