如何推送在 mongoose 上创建的对象以填充另一个模式
How to push an object created on mongoose to populate another schema
我在 MongoDb 上有两个模型,一个用于用户,另一个用于事件。用户创建帐户并登录后,它会显示受保护的页面,可以在其中将事件添加到他们自己的个人资料中。我正在尝试使用 populate("events") 来引用要在用户模式上显示的事件模式。还有 $push 在创建后将事件推送给用户。结果是:事件创建得很好,但没有任何内容被推送到用户模型上的事件数组。使用邮递员查看用户,它显示事件数组为空,我得到的响应是 200 和一个空对象。我在这里错过了什么?这是我第一次在 MongoDb 上关联模式,但无法正常工作。非常感谢任何帮助。
我尝试在 {new: true } 之后添加一个回调函数,还有 {safe: true, upsert: true},但没有任何变化。
这是我的一些代码:
用户模型:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const userSchema = new Schema({
username: { type: String, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
phone: { type: String },
password: { type: String },
email: { type: String, required: true },
events: [{ type: Schema.Types.ObjectId, ref: "Event" }]
});
const User = mongoose.model("User", userSchema);
module.exports = User;
事件模型:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const eventSchema = new Schema({
title: { type: String, required: true },
start: { type: Date, required: true },
end: { type: Date, required: true },
appointment: { type: String, required: true }
});
const Event = mongoose.model("Event", eventSchema);
module.exports = Event;
创建事件然后尝试将创建的对象推送到用户模式的路由:
router.post("/users/:_id", function(req, res) {
Event.create({
title: req.body.title,
start: req.body.start,
end: req.body.end,
appointment: req.body.appointment
})
.then(function(dbEvent) {
return User.findOneAndUpdate(
{ _id: req.params._id },
{
$push: {
events: dbEvent._id
}
},
{ new: true }
);
})
.then(function(dbUser) {
res.json(dbUser);
})
.catch(function(err) {
res.json(err);
});
});
获取一个用户,但它 returns 用户的事件数组为空。
router.get("/users/:_id", (req, res) => {
return User.findOne({
_id: req.params._id
})
.populate("events")
.then(function(dbUser) {
if (typeof dbUser === "object") {
res.json(dbUser);
}
});
});
提前致谢。
问题是我在不同的文件中有事件路由和用户路由,而我忘记将用户模型导入事件路由:
const User = require("../../models").User;
我在 MongoDb 上有两个模型,一个用于用户,另一个用于事件。用户创建帐户并登录后,它会显示受保护的页面,可以在其中将事件添加到他们自己的个人资料中。我正在尝试使用 populate("events") 来引用要在用户模式上显示的事件模式。还有 $push 在创建后将事件推送给用户。结果是:事件创建得很好,但没有任何内容被推送到用户模型上的事件数组。使用邮递员查看用户,它显示事件数组为空,我得到的响应是 200 和一个空对象。我在这里错过了什么?这是我第一次在 MongoDb 上关联模式,但无法正常工作。非常感谢任何帮助。
我尝试在 {new: true } 之后添加一个回调函数,还有 {safe: true, upsert: true},但没有任何变化。
这是我的一些代码:
用户模型:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const userSchema = new Schema({
username: { type: String, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
phone: { type: String },
password: { type: String },
email: { type: String, required: true },
events: [{ type: Schema.Types.ObjectId, ref: "Event" }]
});
const User = mongoose.model("User", userSchema);
module.exports = User;
事件模型:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const eventSchema = new Schema({
title: { type: String, required: true },
start: { type: Date, required: true },
end: { type: Date, required: true },
appointment: { type: String, required: true }
});
const Event = mongoose.model("Event", eventSchema);
module.exports = Event;
创建事件然后尝试将创建的对象推送到用户模式的路由:
router.post("/users/:_id", function(req, res) {
Event.create({
title: req.body.title,
start: req.body.start,
end: req.body.end,
appointment: req.body.appointment
})
.then(function(dbEvent) {
return User.findOneAndUpdate(
{ _id: req.params._id },
{
$push: {
events: dbEvent._id
}
},
{ new: true }
);
})
.then(function(dbUser) {
res.json(dbUser);
})
.catch(function(err) {
res.json(err);
});
});
获取一个用户,但它 returns 用户的事件数组为空。
router.get("/users/:_id", (req, res) => {
return User.findOne({
_id: req.params._id
})
.populate("events")
.then(function(dbUser) {
if (typeof dbUser === "object") {
res.json(dbUser);
}
});
});
提前致谢。
问题是我在不同的文件中有事件路由和用户路由,而我忘记将用户模型导入事件路由: const User = require("../../models").User;