我试图通过NodeJS将对象数组推入MongoDB。
所以我的架构
var UserSchema = new mongoose.Schema({
id: mongoose.Schema.ObjectId,
added: {
type: Date,
default: Date.now()
},
displayName: String,
login: String,
email: String,
phone: String,
password: String,
salt: String,
role: Number,
hasPremium: Boolean,
avatar: Buffer,
description: String,
additional: [{
name: String,
data: String
}],
cart: [{
exposition: Object,
offer: Object,
state: Number,
history: [{
date: Date,
state: Number,
modifier: Number
}]
}],
balance: Number,
topUps: [{
orderNumber: String,
sum: Number,
added: Date,
paid: Date
}],
lock: Boolean
});
我的保存控制器
module.exports = function (passport) {
passport.use('signup', new LocalStrategy({
passReqToCallback: true // allows us to pass back the entire request to the callback
},
function (req, username, password, done) {
findOrCreateUser = function () {
// find a user in Mongo with provided username
UserModel.findOne({'login': username}, function (err, user) {
// In case of any error, return using the done method
if (err) {
console.log('Error in SignUp: ' + err);
return done(err);
}
// already exists
if (user) {
console.log('User already exists with username: ' + username);
return done(null, false, req.flash('message', 'User Already Exists'));
} else {
// if there is no user with that email
// create the user
var newUser = new UserModel();
// set the user's local credentials
newUser.displayName = req.param('displayName');
newUser.login = username;
newUser.password = createHash(password);
newUser.email = req.param('email');
newUser.phone = req.param('phone');
newUser.role = req.param('role');
newUser.description = req.param('description');
if (req.param('avatar')) {
var avatar = new Buffer(req.param('avatar')).toString('base64');
newUser.avatar = new Buffer(avatar, 'base64');
}
var adds = req.param('additional');
console.log(adds);
if (adds) {
newUser.additional = [];
for (var i = 0; i < adds.length; i++) {
newUser.additional[i] = {};
newUser.additional[i].name = adds[i].name;
newUser.additional[i].data = adds[i].data;
}
}
console.log(newUser.additional);
// save the user
newUser.save(function (err) {
if (err) {
console.log('Error in Saving user: ' + err);
throw err;
}
console.log('User Registration succesful');
return done(null, newUser);
});
}
});
};
// Delay the execution of findOrCreateUser and execute the method
// in the next tick of the event loop
process.nextTick(findOrCreateUser);
})
);
// Generates hash using bCrypt
var createHash = function (password) {
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
}
}
所以,当我运行这段代码时,我得到了一个奇怪的错误
TypeError: doc.validate is not a function
即使我的计划中没有验证。
我做错了什么?
谢谢
答案 0 :(得分:0)
This is the solution for your problem.. Hope its helpful
<!--- HTML -->
<ul class="dropdown-menu">
<li><a href="#">Main menu</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a>
<ul class="dropdown-submenu">
<li><a href="#">Sub menu</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
<!-- End HTML -->
/** CSS **/
.dropdown-menu{
margin: 0;
padding: 10px;
list-style: none;
background-color: #dddddd;
max-width:300px;
width: auto;
display: inline-block;
margin-left: 200px;
position:relative;
}
.dropdown-menu li , .dropdown-submenu li{
padding: 5px;
}
.dropdown-menu li a{
text-decoration: none;
color: #ff0000;
}
.dropdown-submenu li a{
text-decoration: none;
color: #fff;
}
.dropdown-submenu {
position: absolute;
list-style:none;
background-color: red;
color:#fff;
margin:0;
padding: 10px;
z-index:2;
width: 100%;
top:0;
right:100%;
}
/** End CSS **/
这部分代码的内容
newUser.additional = req.param('additional');
完美无缺。对我来说还有魔力......