我自己有一个关于Sails.js版本0.10-rc5中的关联的问题.我一直在构建一个应用程序,其中多个模型相互关联,我已到达我需要以某种方式嵌套关联的点.
有三个部分:
首先是博客文章,这是由用户编写的.在博客文章中,我想显示关联用户的信息,如用户名.现在,这里一切正常.直到下一步:我正在尝试显示与帖子相关的评论.
注释是一个单独的模型,称为Comment.每个人还有一个与之相关的作者(用户).我可以轻松地显示评论列表,但是当我想显示与评论相关的用户信息时,我无法弄清楚如何使用用户的信息填充评论.
在我的控制器中,我试图做这样的事情:
Post
.findOne(req.param('id'))
.populate('user')
.populate('comments') // I want to populate this comment with .populate('user') or something
.exec(function(err, post) {
// Handle errors & render view etc.
});
Run Code Online (Sandbox Code Playgroud)
在我的Post''show'动作中,我试图检索这样的信息(简化):
<ul>
<%- _.each(post.comments, function(comment) { %>
<li>
<%= comment.user.name %>
<%= comment.description %>
</li>
<% }); %>
</ul>
Run Code Online (Sandbox Code Playgroud)
但是,comment.user.name将是未定义的.如果我尝试访问'user'属性,例如comment.user,它将显示它的ID.这告诉我,当我将评论与其他模型关联时,它不会自动将用户的信息填充到评论中.
任何理想的人都能妥善解决这个问题:)?
提前致谢!
PS
为了澄清,这就是我基本上在不同模型中建立关联的方式:
// User.js
posts: {
collection: 'post'
},
hours: {
collection: 'hour'
},
comments: {
collection: 'comment'
}
// Post.js
user: {
model: 'user'
},
comments: …
Run Code Online (Sandbox Code Playgroud) 我目前正在我的应用程序中创建一个文件上传系统.我的后端是Sails.js(10.4),它作为我单独的前端(Angular)的API.
我选择将我上传的文件存储到我的MongoDB实例,并使用sails的内置文件上传模块Skipper.我正在使用适配器skipper-gridfs(https://github.com/willhuang85/skipper-gridfs)将文件上传到mongo.
现在,上传文件本身不是问题:我在我的客户端上使用dropzone.js,它将上传的文件发送到/ api/v1/files/upload.文件将上传.
为了实现这一点,我在FileController中使用以下代码:
module.exports = {
upload: function(req, res) {
req.file('uploadfile').upload({
// ...any other options here...
adapter: require('skipper-gridfs'),
uri: 'mongodb://localhost:27017/db_name.files'
}, function(err, files) {
if (err) {
return res.serverError(err);
}
console.log('', files);
return res.json({
message: files.length + ' file(s) uploaded successfully!',
files: files
});
});
}
};
Run Code Online (Sandbox Code Playgroud)
现在问题是:我想在上传文件之前先处理这些文件.特别是两件事:
我不知道从哪里开始或如何实现这种功能.所以任何帮助将不胜感激!
sails.js ×2
associations ×1
file-upload ×1
gridfs ×1
mongodb ×1
nested ×1
node.js ×1
populate ×1
skipper ×1