我一直在努力和阅读安静一段时间,似乎无法找到我的问题的合理解释。
这是我的模式声明的代码:
var mongoose = require('mongoose');
var Schema = mongoose;
var exports = module.exports = {};
var userSchema = new mongoose.Schema({
username: String,
email: String,
password: String
});
var messageSchema = new mongoose.Schema({
text: String,
user: userSchema,
created_at: {type: Date, default: Date.now}
});
var roomSchema = new mongoose.Schema({
name: String,
topic: String,
users: [userSchema],
messages: [messageSchema],
created_at: {type: Date, default: Date.now}
});
var User = mongoose.model("user", userSchema);
var Message = mongoose.model("message", messageSchema);
var Room = mongoose.model("room", roomSchema);
exports.Users = User;
exports.Messages = Message;
exports.Rooms = Room;
然后我只是创建一些虚拟数据:
var usersArr = [];
var userTemp = new User({
username: "Spiderman",
email: "spiderman@chat.com",
password: "password"
});
userTemp.save(function (err, user) {
if (err)return console.log(err)
//console.log(user);
});
usersArr.push(userTemp);
//some more users....
//messagesArr
var messagesArr = [];
var msgTemp = new Message({
text: "I think Magneto is getting too old",
user: usersArr[19]
});
msgTemp.save(function (err, msg) {
if (err)return console.log(err)
//console.log(msg);
});
messagesArr.push(msgTemp);
//some more msgs......
var roomsArr = [];
var roomTemp = new Room({
name: "Villains",
topic: "Discussing newly spotted villains on our streets",
users: usersArr,
messages: messagesArr
});
roomTemp.save(function (err, room) {
if (err)return console.log(err)
// console.log(room);
});
roomsArr.push(roomTemp);
//some more rooms........
除非我尝试传递<{1}}
,否则一切都很美妙messagesArr
然后一切都崩溃了
value.validate(fn,{__ noPromise:true}); ^
TypeError:value.validate不是函数
对我而言,它看起来与我正在使用的其他模式非常相似,我无法弄清楚这个有什么问题。任何帮助都感激不尽!看起来我真的错过了一些东西:/
答案 0 :(得分:0)
var roomsArr = []; var roomTemp = new Room();
roomTemp.name = "Villains";
roomTemp.topic = "Discussing newly spotted villains on our streets";
roomTemp.users = usersArr;
roomTemp.messages.push({msg:messagesArr}); // I did chenages here
roomTemp.save(function (err, room) {
if (err)return console.log(err) // console.log(room); }); roomsArr.push(roomTemp);
I have done changes in your roomTemp object and saving it, now its
should work
答案 1 :(得分:0)
这里有最终的工作代码......
var mongoose = require('mongoose'); var Schema = mongoose.Schema;
var exports = module.exports = {};
var userSchema = new Schema({
username: String,
email: String,
password: String });
var messageSchema = new Schema({
text: String,
//user: userSchema,
user:Object,
created_at: {type: Date, default: Date.now} });
var roomSchema = new Schema({
name: String,
topic: String,
users:[userSchema],
messages:[messageSchema],
created_at: {type: Date, default: Date.now} });
var User = mongoose.model("user", userSchema);
var Message = mongoose.model("message", messageSchema);
var Room = mongoose.model("room", roomSchema);
exports.Users = User;
exports.Messages = Message;
exports.Rooms = Room;
require("./config/monDB");
require('./SOFmodel');
var mongoose = require("mongoose");
var User = mongoose.model("user");
var Message = mongoose.model("message");
var Room = mongoose.model("room");
var usersArr = [];
var userTemp = new User({
username: "Spiderman",
email: "spiderman@chat.com",
password: "password" });
userTemp.save(function (err, user) {
if (err)return console.log(err)
//console.log(user); });
usersArr.push(userTemp);
console.log(usersArr[0]); //some more users....
//messagesArr
var messagesArr = [];
var msgTemp = new Message({
text: "I think Magneto is getting too old",
user: usersArr[0] });
msgTemp.save(function (err, msg) {
if (err)return console.log(err)
//console.log(msg); });
messagesArr.push(msgTemp);
//some more msgs......
console.log(messagesArr);
var roomsArr = [];
var roomTemp = new Room();
roomTemp.name = "Villains";
roomTemp.topic = "Discussing newly spotted villains on our streets";
roomTemp.users = usersArr;
roomTemp.messages.push({msg:messagesArr});
roomTemp.save(function (err, room) {
if (err)return console.log(err) // console.log(room); }); roomsArr.push(roomTemp);