I have a model which looks like this:
我有一个看起来像这样的模型:
User.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var memberSchema = new Schema({
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true,
min: 8
}
});
var userSchemaPrimary = new Schema({
team_code : {
type: String,
required: true,
unique: true
},
members:[memberSchema],
});
var User = mongoose.model('User', userSchemaPrimary);
module.exports = User;
And this is how am trying to save
这就是我想要拯救的方式
var User = require('../models/user');
var newTeam = new User({
team_code : 'CODE01',
members:
{
email: req.body.email,
password: pass
}
});
newTeam.save(function(err) {
if (err) throw err;
console.log('User saved successfully!');
return res.send("Done");
});
When executed, throws model validation error. Well I tried to save data without the array documents, then its saves successfully. But when I try to save the array (array "members"), it throws validation error.
执行时,抛出模型验证错误。好吧,我试图在没有数组文档的情况下保存数据,然后成功保存。但是当我尝试保存数组(数组“成员”)时,它会抛出验证错误。
I WANT TO
我要
Store data in the following way:
以下列方式存储数据:
{
team_code: "CODE01",
members: [
{
email: "test01@email.com",
password: "11111111"
},
{
email: "test02@email.com",
password: "22222222"
}
{
email: "test03@email.com",
password: "33333333"
}
]
}
I dont understand what is going wrong. Any help is appreciated.
我不明白出了什么问题。任何帮助表示赞赏。
1 个解决方案
#1
1
You are assigning object to members
field, but it's an array
您正在将对象分配给成员字段,但它是一个数组
var newTeam = new User({
team_code : 'CODE01',
members: [{
email: req.body.email,
password: pass
}] // <-- note the array braces []
});
#1
1
You are assigning object to members
field, but it's an array
您正在将对象分配给成员字段,但它是一个数组
var newTeam = new User({
team_code : 'CODE01',
members: [{
email: req.body.email,
password: pass
}] // <-- note the array braces []
});