【问题标题】:Querying from models with HasManyThrough relations - strongloop api从具有 HasManyThrough 关系的模型中查询 - strongloop api
【发布时间】:2015-12-12 07:18:54
【问题描述】:

这是对之前question 的后续跟进。目前,该api可以从共享关系的categorygame模型中查询。例如,通过此端点/Categories/1001/games/mature,我可以列出所有fighting 类别的游戏,其中mature 设置为true。但是,我已经包含了来自 db 表 game_info 的第三个模型 gameInfo。因为,我想从这三个表中获取信息,所以我从 db 表 games_categories_bridge 中包含了一个名为 gamesCategoriesBridge 的直通模型。我按照指南设置HasManyThrough relations。问题是descriptionpublishedDate 等附加信息不会显示在最终结果中。如何正确设置remoteMethod 来完成以下任务?

common/models/category.js

module.exports = function(Category) {
Category.mature = function(id, callback) {
    var app = this.app;
    var Game = app.models.Game;
    Game.find({
        "where": {
            categoryId: id,
            mature: true
        }
    }, function(err, gameArr) {
        if (err) return callback(err);
        console.log(gameArr);
        callback(null, gameArr);
    });
}

Category.remoteMethod(
    'mature', {
        accepts: [{
            arg: 'id',
            type: 'number',
            required: true
        }],
        // mixing ':id' into the rest url allows $owner to be determined and used for access control
        http: {
            path: '/:id/games/mature',
            verb: 'get'
        },
        returns: {
            arg: 'games',
            type: 'array'
        }
    }
);

};

表架构:

类别

category_name       category_id 
-------------       -----------
fighting            1001
racing              1002
sports              1003

游戏

game_id         game_name           category_id     mature
-----------     ------------        -----------     --------------
13KXZ74XL8M     Tekken              10001           true
138XZ5LPJgM     Forza               10002           false

游戏信息

game_id         description                     published_date
-----------     -----------                     --------------                      
13KXZ74XL8M     Published by Namco.             1994
138XZ5LPJgM     Published by Microsoft Studios. 2005

games_categories_bridge

game_id        category_id   
-----------    ----------- 
13KXZ74XL8M    10001   
138XZ5LPJgM    10002 

端点:/categories/{id}/games/mature API 响应的所需格式:

games [ 
{
gameName: 'Tekken', 
gameInfo : 
[
    { 
        description : 'Published by Namco.',
        published_date : '1994'
    }
],
        categorName: 'fighting', 
        categoryId: 1001, 
        mature: true 
}
.....
]

【问题讨论】:

  • 您想要的 API 响应格式具有 game_info 属性,但它显示了与 category 相关的信息,为什么会这样。另外,我认为您需要重新设计数据库的架构。因为使用这个模式你可以实现你想要的,但事情会变得比平常困难。
  • @RobinsGupta,对不起,错字。更正了所需的 API 响应。
  • games->category model 之间是什么关系?每个game 属于一个category 还是每个game 可以有多个category
  • @RobinsGupta 每个游戏可以有很多类别

标签: node.js loopbackjs strongloop


【解决方案1】:

首先在gamegame_info模型之间创建hasMany关系

//Now inside remote_method.
Category.mature = function(id, callback) {
    var app = this.app;
    var Game = app.models.game;
    Category.findById(id, {}, function(err, category) {
        if (err) return callback(err);
        //Now call the Game find method
        Game.find({
            "where": {
                categoryId: id,
                mature: true
            },
            include:'game_info'
        }, function(err, gameArr) {
            if (err) return callback(err);
            gameArr.forEach(function(gameObj, index){
                gameObj.categoryName = category.category_name;

            });
            callback(null, gameArr);
        });
    });
}

【讨论】:

  • 对于第一个 where 我收到错误 ER_BAD_FIELD_ERROR: Unknown column 'category_id id' in 'where clause'
  • @Code_Ed_Student 有一个错字我已经编辑检查。
  • 您是否能够将社交登录身份验证与环回结合起来? stackoverflow.com/questions/34549717/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
相关资源
最近更新 更多