我正在尝试填充用户内的嵌套对象。我正在尝试使用EJS标记显示所有收到的消息,以及消息时间,主题行和作者。我只能填充给定消息的ID。 这是我的数据结构
User
--threads
--thread_id
--messages
--sent
--message_id
--recieved
--message_id
这是我的模式:
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var uniqueValidator = require('mongoose-unique-validator');
var UserSchema = mongoose.Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
first_name: { type: String, required: true },
middle_initial: { type: String, required: true },
last_name: { type: String, required: true },
phone: { type: Number, required: true },
address: { type: String, required: true },
email: { type: String, required: true, unique: true },
isManager: Boolean,
isSysAdmin: Boolean,
points_accrued: Number,
threads: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Thread"
}
],
messages: {
sent: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Message"
}],
recieved: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Message"
}],
},
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
});
UserSchema.plugin(passportLocalMongoose);
UserSchema.plugin(uniqueValidator);
module.exports = mongoose.model("User", UserSchema);
线程模式:
var mongoose = require("mongoose");
var threadSchema = mongoose.Schema({
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Thread"
},
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
name: { type: String, required: true }
},
recipient: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
name: { type: String, required: true }
},
messages: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Message"
}
],
subject: { type: String, required: true },
date: { type: String, required: true },
time: { type: String, required: true },
});
module.exports = mongoose.model("Thread", threadSchema);
最后是消息模式:
var mongoose = require("mongoose");
var messageSchema = mongoose.Schema({
text: { type: String, required: true },
subject: { type: String, required: true },
date: { type: String, required: true },
time: { type: String, required: true },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
name: { type: String, required: true }
}
});
module.exports = mongoose.model("Message", messageSchema);
现在这是我目前正在使用的GET路由,应该用来填充消息:**注意,我的代码中对所有模型都具有所有正确的require语句
// Gets messages for regular Employees, not Admins
router.get("/", middleware.isLoggedIn, function (req, res) {
User
.findById(req.user._id)
.populate(/////IDK WHAT SHOULD GO HERE/////)
.exec(function (err, foundUser) {
if (err || !foundUser) {
console.log("This is an error", err);
req.flash("error", "User Not Found");
res.redirect("back");
} else {
User.find({}, function (err, foundUsers) {
if (err || !foundUsers) {
console.log(err);
req.flash("error", "Users Not Found");
res.redirect("back");
} else {
res.render("users/messages", { users: foundUsers, user: foundUser });
}
});
}
});
})
最后,我的EJS文件:
<hr>
<%= user%>
<% user.messages.recieved.forEach(function(message) {%>
<p>
<%= message%>
<%= message.text%>
</p>
<% }); %>
所以在我的ejs中,再次显示的是ID,但是没有出现message.text。我尝试了许多不同的方法来显示message.text。这是我尝试过的:
.populate("messages") //DIDNT WORK
.populate({
path: "messages",
select: "sent recieved"
}) //DIDNT WORK
.populate({
path: "messages",
select: "sent recieved",
populate: {
path: "sent",
model: "Message",
select: "text subject ect..."
}
}) //DIDNT WORK
如果您能帮助我,并解释您给我的答案,我将非常感激!