簡體   English   中英

.map 使用 Mongoose 獲取數據后返回空對象數組

[英].map returns an array of empty objects after getting data with Mongoose

當我想要使用 Mongoose 將一組集合返回給我時,我遇到了問題。 問題是代碼中的 .map 方法返回一個空對象數組,但如果我在 .map 中單獨記錄對象,一切都很好。 為什么會這樣?

const patients = doctor.patients.map(async patient => {
  try {
    const patientObj = await Patient.findOne({ username: patient });
    patient = patientObj;
    patient.jwt = undefined;
    patient.__v = undefined;
    console.log(patient);  // This works just fine, logs the object the right way

    return patient;
  } catch (err) {
    console.log(err);
  }
});
console.log(patients); // This logs [{}, {}, {}]

我猜你想擁有一系列與一位醫生有關的患者。 試試這個解決方案。

Patient.find({
    username: { $in: doctor.patients }
}, (err: any, patients) => {
    console.log("patients  " + patients)
})

在您的患者模型中添加(選擇:false),因此您不必將每個字段都設置為未定義https://mongoosejs.com/docs/api.html#schematype_SchemaType-select

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM