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
});
}
});