繁体   English   中英

在 NodeJs 和 MongoDB 问题中需要帮助(Mongoose 填充不工作)

[英]Need help in NodeJs and MongoDB problem (Mongoose populate is not working)

这是我的模式

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const finalDocument = new Schema({
  name: {
    type: String,
  },
  Properties: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Properties",
  },
  borrower: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "borrowers",
  },
});

var FinalDocuments = mongoose.model("finalDocument", finalDocument);
module.exports = FinalDocuments;

这是我的路线。

finalDocument
    .find({})
    .populate("borrower")
    .exec()
    .then((brws) => {
      res.statusCode = 200;
      res.setHeader("Content-Type", "application/json");
      res.json(brws);
    })
    .catch((err) => {
      console.log("error in getting borrowers", err);
    });

当我打电话给这条路线时,没有人填写借款人。 我附上Postman的照片。

您的代码似乎没有问题。

可能的原因是:

  1. 您在ref => borrower: { type: mongoose.Schema.Types.ObjectId, ref: "borrowers", }中提到的集合名称在您的架构中可能是拼写错误或定义了错误的架构名称
  2. 可能是借款人催收没有文件
  3. 更新您的 mongoose 版本并检查

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM