Search code examples
javascriptnode.jsloopbackjs

LoopBack access another model


This is the situation:

I got my database with 3 models:

Pokemon / PokemonType / Type

A Pokemon is linked through the PokemonType to one or more Types.

So now I want a function to get the Types linked to the given Pokemon:

module.exports = function(Pokemon) {


    Pokemon.types = function(id, cb){
        var PokemonType = Pokemon.app.models.pokemonType.find();
        var Type = Pokemon.app.models.type.find();
        var arrayTypes = [];

        // var result = app.models.pokemonType.find({where : {"pokemon_id": id}});
//      for(var x in result){
//          var typeId = x.pokemonType_id;
//          var results = Type.find({where: {"id": typeId}});
//          arrayTypes.push(results);
//      }
        cb(null, PokemonType[0] + " AND " + Type);
    }

    Pokemon.remoteMethod(
        'types', {
            accepts: {arg: 'id', type:'number', required:true, http: { source: 'header' }},
            returns: {arg: 'types', type:'object' },
            http: {path: '/types', verb: 'get', status: 200, errorStatus: 400}
        }   
    );
};

I'm trying to find the types from the result of the PokemonType table. So if there is 1 row with the current PokemonID, I want the type belonging to this PokemonType row out of the Type table.

However when trying to do this with the models, i keep getting undefined and [object Object].

I know that if I use Pokemon.find it uses the table to search for the Pokemon, but does it have the same instance if I search for the PokemonType with the given PokemonID? Or am I thinking completely wrong?

What do I mean? I expect this:

PokemonType.find({where :{ "pokemon_id": id}}); 

to act the same as:

Pokemon.find({where: { "id": id}});

but with the correct answer.

How do I fix the undefined issue? And how do I then get the correct Types linked to the Pokemon?


Solution

  • Solution found:

    Pokemon.types = function(id, cb){
            var PokemonType;
            var curPokemon = {"pokemon":null, "type": null};
            var Type;
            var arrayTypes = new Array();
            var count;
    
            Pokemon.find({where: {"id": id}})
            .then(result => {
                if(result.length > 0){
                    curPokemon.pokemon = result[0];
                    Pokemon.app.models.pokemonType
                    .find({where:{"pokemon_id": id}})
                    .then(result => {
                        count = result.length
                        result.forEach(function(item, index){
                            Pokemon.app.models.type
                            .find({where:{"id": item.type_id}})
                            .then(result => {
                                if(result.length > 0){
                                    arrayTypes.push(result[0]);
                                    if(arrayTypes.length == count){
                                        curPokemon.type = arrayTypes;
                                        cb(null, curPokemon);
                                    }
                                }
                            });
                        });
                    });
                }
            });
        }
    

    By using MainModel.app.models.ModelIWant I can get the correct model instance. Then using the .find and use my logic inside the promise (.then).

    This allowed me to get the correct results and then use these results to get the correct data. Then by making a own model (object) with the curPokemon, I could modify the result so it would add the wished data to the MainModel.