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?