【问题标题】:LoopBack access another modelLoopBack 访问另一个模型
【发布时间】:2016-04-07 14:14:26
【问题描述】:

情况是这样的:

我的数据库有 3 个模型:

口袋妖怪/口袋妖怪类型/类型

Pokemon 通过 PokemonType 链接到一个或多个类型。

所以现在我想要一个函数来获取链接到给定口袋妖怪的类型:

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

我正在尝试从 PokemonType 表的结果中查找类型。因此,如果当前 PokemonID​​ 有 1 行,我希望将属于此 PokemonType 行的类型从 Type 表中移出。

但是,当尝试对模型执行此操作时,我不断得到 undefined 和 [object Object]。

我知道,如果我使用 Pokemon.find,它会使用该表来搜索 Pokemon,但是如果我使用给定的 PokemonID​​ 搜索 PokemonType,它是否具有相同的实例?还是我的想法完全错误?

我是什么意思?我希望这样:

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

做同样的事情:

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

但答案正确。

如何解决未定义的问题?然后我如何获得与口袋妖怪相关联的正确类型?

【问题讨论】:

    标签: javascript node.js loopbackjs


    【解决方案1】:

    找到的解决方案:

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

    通过使用 MainModel.app.models.ModelIWant 我可以获得正确的模型实例。然后使用 .find 并在 promise (.then) 中使用我的逻辑。

    这使我能够获得正确的结果,然后使用这些结果来获得正确的数据。然后通过使用 curPokemon 创建一个自己的模型(对象),我可以修改结果,以便将所需的数据添加到 MainModel。

    【讨论】:

    • 能否请您添加更多详细信息...我也卡在这里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2019-09-07
    相关资源
    最近更新 更多