mongoose 可能是什么问题呢?在我发布之后,我在数据库中的数据不包括提供者的ID,并且我在模式中指定了t作为字段

8wigbo56  于 2023-06-23  发布在  Go
关注(0)|答案(1)|浏览(69)

我在一个nodejs项目中使用mongoDB的mongoose ORM,并且有这个模式,我试图确保医生可以为患者书写病史,患者也可以沿着其他字段对doctor进行评分以获取患者的信息:

const recipientSchema = new Schema({
      ...
      history: [{
        provider: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'Provider',
          required: true
        },
        symptoms: { type: String, required: true },
        diagnosis: { type: String, required: true },
        notes: { type: String, required: true },
        prescriptions: { type: String, required: true },
        createdAt: { type: Date, required: true, default: Date.now }
      }],
      ratings: [{
        doctor: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'Provider',
        },
        score: {type: Number, default: 0}
      }],
    }, { timestamps: true });

我也有这个控制器来将患者的历史记录发布到数据库:

async function postHistory(req, res){
      try{
        const { providerId, symptoms, diagnosis, notes, prescriptions } = req.body;

        // Update the recipient's history record
        const recipient = await Recipient.findById(req.params.id);
        recipient.history.push({
          provider: providerId,
          symptoms,
          diagnosis,
          notes,
          prescriptions
        });
        await recipient.save();

        return res.status(201).json({ message: 'History submitted successfully.' });
      } catch (error) {
        console.error(error);
        return res.status(500).json({ message: 'An error occurred while submitting the history.'});
      }
    });

这是我的模拟:

{
    "provider": "6487108816960b932819019e",
    "symptoms": "test",
    "diagnosis": "test",
    "notes": "test",
    "prescriptions": "test"
}

这是数据库里的内容:

{  ...  
"history": [{
"symptoms": "test",
"diagnosis": "test",
"notes": "test",
"prescriptions": "test",
...
}
],
...
}

缺少提供程序字段!我试过删除和重新发布,但结果仍然是一样的。
我期待的是这样的:

{  ...  
"history": [{
"provider": ObjectId("64878a7e965a33c829585f68"),
"symptoms": "test",
"diagnosis": "test",
"notes": "test",
"prescriptions": "test",
...
}.

],
...
}

如果你查看模式,你会看到我有两个ref,另一个ref(rates)没有问题,每个字段都反映了我发布到数据库的时间。

pb3s4cty

pb3s4cty1#

您的mock与postHistoryreq.body中期望的内容不匹配:

// expected is `providerId`...
const { providerId, symptoms, diagnosis, notes, prescriptions } = req.body;
        ^^^^^^^^^^

// ...but you're mocking with `provider`
"provider": "6487108816960b932819019e",
 ^^^^^^^^

相关问题