Javascript object does not create new Keys

Asked

Viewed 33 times

2

I am using Mongodb and returns me all the right data but when I want to add a new Key simply 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 validation the result is this, in both the if and the other.

{ _id: 59fa1a69349c9f382f0a2892,
   nome: 'qqqqqqq',
   telefone: '189789469',
   curso: 'eeeeee',
   email: 'teste',
   senha: 2651
   codigo: 2,
__v: 0 }
  • And where is the code for creation? You can post it?

  • You are referring to the bank creation code ?

  • Exact, the code that generates the error (failure to create the key), unless I misunderstood your question

  • It shows no error code, I try to assign the key status and in the console.log I get the key status does not exist.

  • If you do var usuario = Object.merge({}, logins[0]); already works?

1 answer

1


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

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

Note: In this example of mine, userCopy is not really a copy. change it changes user data. _doc.

  • 1

    The good now I understood better as Mongo returns me the data, I’m starting with Mongo and do not understand well yet, thanks

Browser other questions tagged

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