Mongoose populate正在填充错误的文档

时间:2018-03-30 21:29:20

标签: mongodb mongoose

我正在与MongoDB和Mongoose构建一个反应应用程序。我有看护人和病人。

我在登录后抓住看护人ID并使用它来填充"患者"与该看守相关联。但出于某种原因,我无法弄清楚为什么我的看守人系列本身(Caretaker)而不是来自"患者"集合。

患者模型:

const patientSchema = new Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    address: {type: String, required: true},
    address2: {type: String},
    city: {type: String, required: true},
    state: {type: String, required: true},
    zip: {type: String, required: true},
    preferredLanguage: {type: String, required: true},
    medicalCond: {type: String, required: true},
    medication: {type: String},
    allergies: {type: String},
    notes: {type: String},
    primaryCareName: {type: String},
    primaryCareNumber: {type: String},
    contactFirstName: {type: String, required: true},
    contactLastName: {type: String, required: true},
    contactRelationship: {type: String, required: true},
    contactNumb: {type: String, required: true}
});

看守模型:

const caretakerSchema = new Schema({
    username: {type: String, unique: true, required: true},
    password: {type: String, required: true},
    phoneNumb: {type: String, required: true},
    local: {
         username: {type: String, unique: true},
         password: {type: String, unique: true}
     },

    patient: [{
         type: Schema.Types.ObjectId,
         ref: "Patient"
    }]
 });

在上述患者密钥中,它填写了非患者登记的看护人的价值。

这是我的代码.populate

findById: function(req, res) {
    db.Caretaker
        .findOne({_id: req.params.id})
        .populate("patient")
        .then(dbModel => res.json(dbModel))
        .catch(err => res.status(422).json(err));
}

1 个答案:

答案 0 :(得分:0)

所以在你的例子中你:

  1. 在Caretaker上搜索_id = req.params.id
  2. 询问Caretaker患者的病人收集
  3. 创建json
  4. Calling handler.removeCallbacks from onDeleted method in a widget throws a nullpointerexception