【问题标题】:Add custom attribute to waterline model将自定义属性添加到水线模型
【发布时间】:2016-06-06 09:51:54
【问题描述】:

由于嵌套人口不可用,我需要手动传递我的自定义属性。在我的具体情况下,这意味着:一个客户有很多项目,一个项目有很多贡献者。

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 仍未定义。为什么?以及如何附加这些自定义属性?

【问题讨论】:

    标签: javascript node.js sails.js


    【解决方案1】:

    您的代码中有很多错误。

    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
          });
        });
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 2020-11-23
      • 2015-09-23
      • 1970-01-01
      • 2011-02-18
      相关资源
      最近更新 更多