I want to insert cont and cont_type inside pro_picture. But this is not a embedded schema.
My schema:
var userSch = new Schema({
name: String,
email: String,
profile_picture: {
cont: String,
cont_type: String
}
});
var User = mongoose.model('user', userSch);
Now I want to find the user whose email is "something@example.com' and update the pro_picture.
I tried the following code.
User.findOne({email: data.email}, function (err, user) {
var userData = new User({
profile_picture: {
cont: data.pro_cont,
cont_type: data.pro_cont_type
}
});
user.profile_picture = userData;
console.log(user.profile_picture);
console.log(user);
user.save(function (err) {
if(err) throw err;
callback(rjTypes.result.SUCCESS);
});
It shows
profile_picture: null //in console
but the field is not present in collection
Finally i found it..
I solved by using this technique....
User.findOne({email: data.email}, function (err, user) {
user.profile_picture.cont = data.pro_cont;
user.profile_picture.cont_type = data.pro_cont_type;
console.log(user.profile_picture);
console.log(user);
//user.profile_picture.cont_type = data.pro_cont_type;
user.save(function (err) {
if(err) throw err;
callback(rjTypes.result.SUCCESS);
});
});
I think you're doing one nesting to much, try changing
var userData = new User({
profile_picture: {
cont: data.pro_cont,
cont_type: data.pro_cont_type
}
});
to
var userData = {
cont: data.pro_cont,
cont_type: data.pro_cont_type
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.