JavaScript object does not create new keys

2

I'm using MongoDB and it returns me all the right data but when I want to add a new key it just does not create.

exports.validaLogin = function(req, res) {
login.find({
  "nome": req.query.usuario
},function (err, logins) {
  if(err) { 
    return handleError(res, err);
  }
  var usuario = logins[0]
  if(usuario.senha == req.query.senha){
    usuario.status = 1
    console.log("aqui")
  } else {
    console.log("nao")
    usuario.status = 0
  }
  console.log(usuario)
  return res.status(200).json(usuario);
}); };

In my console.log after the validation the result is this, both in the if and in the other.

{ _id: 59fa1a69349c9f382f0a2892,
   nome: 'qqqqqqq',
   telefone: '189789469',
   curso: 'eeeeee',
   email: 'teste',
   senha: 2651
   codigo: 2,
__v: 0 }
    
asked by anonymous 01.11.2017 / 20:13

1 answer

1

The mongo uses a getter for the data. If you want to change the value (or add new data), you should use usuario._doc .

let usuarioCopy = usuario._doc; usuarioCopy.status = 0; console.log(usuarioCopy);

Note: In my example, userCopy is not really a copy. change it changes the user_doc.

    
01.11.2017 / 23:39