Monday 10 April 2017

Node and mongoose - Acces object inside another object

I got this schema:

var mongoose = require('mongoose');


var userSchema  = new mongoose.Schema({

    email: {type:String, unique: true, lowercase: true},

    cpf: String,

    password: String,

    profile:{
        name:{type:String, default:''},
        photo: {type:String, default:''}
    },

    adress:String,

    history:[{
        date: Date,
        paid:{type:Number, default:0},
        item:{type:mongoose.Schema.Types.ObjectId, ref:'Products'}
    }]
});


module.exports = mongoose.model('User', userSchema);

But, when I use User.create({email:req.body.email, password:req.body.password, profile.name: req.body.name}), when I try to pass some value to profile.name, console gives me an error. SyntaxError: Unexpected token .

How can I acess this object and pass some value for him?



via Gustavo Dias

No comments:

Post a Comment