对于我认为你想要做的事有不同的方式。我也建议不要直接使用这些模型,而是使用你想要的服务(通过调用他们的service methods)。你也想熟悉API for hooks。然后,你有一些选择:
1)修改现有的成果
要修改现有服务调用的结果,修改hook.result
在after
钩:
module.exports = function() {
return function (hook) {
let productService = hook.app.service('product');
return productService.find({
paginate: false // Turn off pagination
}).then(products => {
hook.result.products = products;
return hook;
});
};
};
然后
const myHook = require('../../hooks/myhook');
app.service('products-master').hooks({
after: {
get: [ myHook() ]
}
});
这将增加一个products
财产时得到一个唱歌从products-master
服务的项目。
2)设置hook.result
要跳过数据库调用,您可以设置hook.result
到要在before
钩回什么。请记住,所有after
挂钩仍将运行。
3)使用自定义服务
在custom service你可以做任何你喜欢的,例如得到所有你需要的东西,然后手动将它们放在一起:
app.use('/all-products', {
find() {
return this.app.service('products-master').find(params).then(page => {
const promises = page.data.map(masterProduct => {
return this.app.service('products').find({
paginate: false,
query: {
masterProductId: masterProduct._id
}
}).then(products => {
// Add to masterProduct
masterProduct.products = products;
});
});
return Promise.all(promises).then(() => {
// Return `page` which has the modified data
return page;
});
});
},
setup(app) {
this.app = app;
}
});
发电机可以通过选择custom service
并修改servicename.class.js
来创建自定义服务。
4)填入钩
因为我觉得你只是想填充相关的实体,您也可以看看populate common hook。它可以自动完成这些事情。
5)用猫鼬$populate
的Mongoose Feathers adapter还支持$populate query parameter它允许你在你在你的猫鼬模型中定义的关系拉。
欲了解羽毛协会的更多信息,请参阅this FAQ。
谢谢@Daff! – Rahul