我遇到的问题是:
我有两个模式,一个是用户,另一个是消息。
现在,当我尝试将消息从一个用户发布到另一个用户时,会创建一个message.id,但消息和作者的文本不会保存。
以下是我的架构
var mongoose = require("mongoose");
var messageSchema = mongoose.Schema({
text: { type: String },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
name: { type: String }
}
});
module.exports = mongoose.model("Message", messageSchema);
和用户
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,
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
messages: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Message"
}
]
});
UserSchema.plugin(passportLocalMongoose);
UserSchema.plugin(uniqueValidator);
module.exports = mongoose.model("User", UserSchema);
最后这是我的路线
var express = require("express"),
router = express.Router({ mergeParams: true }),
User = require("../models/user"),
Message = require("../models/message"),
middleware = require("../middleware");
//INDEX SHOW USER INFO
router.get("/", middleware.isLoggedIn, function (req, res) {
res.render("users/index");
});
// Gets messages for regular Employees, not Admins
router.get("/messages", middleware.isLoggedIn, function (req, res) {
User.find({}, function (err, allUsers) {
if (err) {
console.log("Error Occured");
console.log(err);
} else {
res.render("users/messages", { users: allUsers });
}
})
});
//================//
// POST A MESSAGE //
//================//
router.post("/messages", middleware.isLoggedIn, function (req, res) {
User.findByUsername(req.body.reciever, function (err, foundUser) {
if(err){
console.log(err, "Could Not Find User");
res.redirect("back");
} else {
console.log("User Found...");
console.log(foundUser);
Message.create(req.body.newMessage, function (err, newMessage) {
if(err){
req.flash("error", "Whoops! Something went wrong...");
console.log(err);
res.redirect("/users/messages");
} else {
newMessage.author.id = req.user._id;
newMessage.author.name = req.user.first_name + " " + req.user.middle_initial + " " + req.user.last_name;
newMessage.save();
// console.log(message.text);
foundUser.messages.push(newMessage);
foundUser.save();
req.flash("success", "Message Sent!");
res.redirect("/users/messages");
}
});
}
});
});
module.exports = router;
现在,当我尝试使用<%= message.text%>我没有收到任何东西,当我在我的ejs文件中尝试console.log(message.text)时,我得到了未定义。当我尝试<%= message%>我得到了消息的ID。最后,在我的路线中,我可以使用console.log(req.body.newMessage)等等,它确实会带回数据。但是,当我尝试类似console.log(foundUser.messages [0] .text)之类的东西时,即使我刚刚在Message和foundUser上使用了.save(),我仍然未定义。我真的很困惑,如果有人可以发光,这将是非常有帮助的。我已经尝试了很多我能做到的事情。谢谢!
UPDATE !!!!!!!
事实证明,Schema正确保存,但我的问题仍然是我无法查看除了消息的ID之外的任何内容。这是数据对象的样子:
{
"_id": {
"$oid": "5b26c60f25d6c9248c91b5ee"
},
"text": "hey",
"__v": 0,
"author": {
"id": {
"$oid": "5b25b6c25813b101b4887dec"
},
"name": "Scott W Selke"
}
}