Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Model.populate()不是Mongoose中的返回文档_Mongodb_Express_Mongoose_Populate_Mongoose Populate - Fatal编程技术网

Mongodb Model.populate()不是Mongoose中的返回文档

Mongodb Model.populate()不是Mongoose中的返回文档,mongodb,express,mongoose,populate,mongoose-populate,Mongodb,Express,Mongoose,Populate,Mongoose Populate,我有两个模式 const mongoose = require("mongoose"); const Schema = mongoose.Schema; // Create the User Schema const UserSchema = new Schema({ email: { type: String }, password: { type: String } }); module.exports = User = m

我有两个模式

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

// Create the User Schema
const UserSchema = new Schema({
    email: {
        type: String
    },
    password: {
        type: String
    }
});

module.exports = User = mongoose.model("users", UserSchema);

然后我在api路径中使用填充:

当我调用这个路由时,我得到了一个空数组[]。。。。知道我做错了什么吗?我应该提到,我已经在状态集合中插入了两个管理员id的记录


还有别的方法吗?

有很多方法

你应该用这个

Status.find({}).then((doc) => {
  if (doc) {
    Status.populate(doc, { path: "admin_id", model: "users" }, function (
      err,
      data
    ) {
      if (err) throw err;
      console.log(data); //here is your documents with admin user
    });
  }
});
router.get(
  "/",
  passport.authenticate("jwt", {
    session: false,
  }),
  (req, res) => {
    try {
      Status.find({}).populate('admin_id').exec(err, data=>{
        console.log(data); // return a blank array : []
        return res.sendStatus(200)
      })
    }
  } catch (error) {
      res.sendStatus(500);
    }
  }
);
Status.find({}).then((doc) => {
  if (doc) {
    Status.populate(doc, { path: "admin_id", model: "users" }, function (
      err,
      data
    ) {
      if (err) throw err;
      console.log(data); //here is your documents with admin user
    });
  }
});