我知道在Sails.js/Waterline中没有内置的方式来填充深层嵌套的关联,所以我试图使用蓝鸟的承诺来实现这一点,但我遇到了一个问题.
我正在成功检索用户,以及与之关联的所有帖子(填充了图像集合)(console.log向我显示所有内容都已正确填充).但是,当我覆盖用户的属性"post"并尝试分配之前检索的完全填充的帖子时,它不能正确填充Post.js的images属性.这就像ORM阻止手动分配Post.js的图像集合.
我究竟做错了什么?填充深层嵌套的一对多关联的最佳方法是什么?
贝娄我已经粘贴了我正在执行的所有代码....
// Populate nested association
nested: function (req, res, next){
var username = req.param("id");
User
.findOneByUsername(username)
.populateAll()
.then(function (user){
var posts = Post.find({
"user": user.id
})
.populate('images')
.populate('category')
.then(function (posts){
return posts;
});
return [user, posts];
})
.spread(function (user, posts){
user.posts = posts; // This won't work.... It assigns all the fields properly but the images collection attribute
res.json(user);
}).catch(function (err){
if (err) return res.serverError(err);
});
}
// --- User.js Model --- //
module.exports = {
attributes: …Run Code Online (Sandbox Code Playgroud)