使用HasManyThrough关系查询模型 – strongloop api

这是前一个问题的后续。 目前,api可以从共享关系的categorygame模型中进行查询。 例如,通过这个端点/Categories/1001/games/mature我可以列出所有已经maturefighting类游戏设置为true 。 不过,我已经从数据库表game_info包含了第三个模型game_info 。 因为我想从这三个表中获取信息,我已经在数据库表games_categories_bridge包含了一个名为gamesCategoriesBridge的贯穿模型。 我遵循指导方针来设置HasManyThrough关系 。 问题在于最终结果中没有显示descriptionpublishedDate等附加信息。 我怎样才能正确设置remoteMethod来完成以下?

通用/模型/ 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' } } ); }; 

表格模式:

catgories

 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 } ..... ] 

首先在gamegame_info模型之间创build一个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); }); }); }