Inserting mongodb documents

2

I have the following document (Schema):

var CandidatoSchema = new Schema ({
    id_login: Schema.Types.ObjectId,
    nome: String,
    cpf: String,
    dataNascimento: Date,
    sexo: String,
    estadoCivil: String,
    endereco: {
        endereco: String,
        numero: Number,
        bairro: String,
        complemento: String,
        cep: String,
        uf: String,
        cidade: String
    }
});

I have a Form with these fields, when the user clicks the Salvar button it will send the data to my cadastro :

on my route record I have a console.log(req.body) : which shows the data sent.

{ nome: 'NomeSs',
  cpf: 'xxx.xxx.xxx-xx',
  endereco:
   { endereco: 'rua alameda',
     numero: '111',
     bairro: 'bairro',
     complemento: 'compras',
     cep: '111-111' }

I'm having difficulty inserting the data into embedded documents, in which case you are not inserting the endereco data.

My role to insert:

exports.Add = function(req, res) {
    console.log(req.body);
    var person = new Person();

    // Tentei usar está linha, mas não funciona...
    person.endereco.push(req.body.endereco);

    person.save(function(err, person) {
        if(err) res.sendStatus(500);
            res.sendStatus(201);
    });
};

Does anyone have any idea how I can do to insert documents embedded ?

    
asked by anonymous 02.04.2016 / 05:18

1 answer

2

First, your address property is not array type for you to push. He is an object type.

If you run person.endereco = req.body.endereco you would be able to assign the value.

But even better, if you do var person = new Person(req.body) already will create the object with all the values assigned.

    
02.04.2016 / 05:36