0

我有一个简单的需要为患者添加标签。我遵循了有关多对多关联的 Sails 和 Waterline 文档,但它在某些时候失败了(没有错误)。我正在使用 MongoDB 进行数据存储。下面的代码:

标签模型

module.exports = {

attributes: {
  name: 'STRING',
  color: {
      type: 'STRING',
      defaultsTo: '#777777'
  },
  tagged: {
      collection: 'patient',
      via: 'tags',
      dominant: true
  }

}
};

患者模型

module.exports = {

attributes: {
    name: 'STRING',
    tags: {
        collection: 'tag',
        via: 'tagged'
    }
}
};

这是尝试关联数据的控制器方法:

module.exports = {

addToPatient: function(req, res) {
    Patient.findOne({id: req.param('patientId')}).exec(function(err, patient) {

        // Queue up a record to be inserted into the join table
        patient.tags.add(req.param('tagId'));

        // Save the user, creating the new associations in the join table
        patient.save(function(err) {});
    });

    res.send("tag assigned");
}
};

我检查了各种休息时间的反应,一切似乎都很好。病人找到了。保存函数在患者对象中显示标签关联,但数据库中没有添加任何内容。我假设我将看到正在创建的连接表或患者/标签集合中的某些内容以表示关联,但我什么也看不到。我很困惑。如果我执行 HTTP 获取,我会看到一个“标签分配”响应。我错过了什么?

4

1 回答 1

0

对我来说效果很好,但是您在标签患者集合中,您不会看到带有关联的填充字段。您将看到创建的新连接集合包含@sgress454 指出的关系。

于 2015-05-15T18:32:26.583 回答