Inserting documents from Mongodb

Asked

Viewed 645 times

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 button Salvar it will send the data to my route cadastro:

in my registration route 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 trouble entering the data into documents embedded, in this case is not entering the data relating to endereco.

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 to insert documents embedded?

1 answer

2


First of all, your property addressee is not an array type for you to be pushing. It is an object type.

If you run person.endereco = req.body.endereco you could assign the value.

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

  • the push I really... thank you very much... I work right here...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.