Javascript 向水线模型添加自定义属性
由于嵌套填充不可用,我需要手动传递自定义属性。在我的具体案例中,这意味着:一个客户有许多项目,一个项目有许多贡献者Javascript 向水线模型添加自定义属性,javascript,node.js,sails.js,Javascript,Node.js,Sails.js,由于嵌套填充不可用,我需要手动传递自定义属性。在我的具体案例中,这意味着:一个客户有许多项目,一个项目有许多贡献者 Customer.find().populate('projects').exec(function(err, customer) { 反应看起来像 [ { "projects": [ { "name": "First project" } ], "customer": "John Doe"
Customer.find().populate('projects').exec(function(err, customer) {
反应看起来像
[
{
"projects": [
{ "name": "First project" }
],
"customer": "John Doe"
},
{
"projects": [
{ "name": "Another project" },
{ "name": "And another one" }
],
"customer": "Susan Doe"
}
]
我正在迭代这些项目,希望附加一个contributors
属性。我试过了
customer.forEach(function(customer, index) {
customer.projects.forEach(function(project, index) {
ProjectContributor.find({
project: project.id
}).exec(function(err, contributor) {
project.contributors = contributors;
});
但是
project.contributors
仍然没有定义。为什么?如何附加这些自定义属性?代码中有许多错误
Customer.find().populate('projects').exec(function(err, customers) {
customers.forEach(function(customer, index) {
customer.projects.forEach(function(project, index) {
ProjectContributor.findOne({project: project.id}) // use findOne since you only want one project at a time
.populate('contributors')
.exec(function(err, projectContributor) {
project.contributors = projectContributor.contributors; // contributors is in projectContributor
});
});
});
});