update on mongodb nodejs

1

I'm trying to put a put on mongodb using the findByIdAndUpdate method but it does not work, it does not show any errors, it just does not update the data. This is my code:

function updateNota(req, res){
  let notaId = req.params.notaId
  let update = req.body
  console.log(update)

  Nota.findByIdAndUpdate(notaId, update, (err, notaUpdated) => {
    if(err) res.status(500).send({message: 'Error al actualizar la nota: ${err}'})

    res.status(200).send({ nota: notaUpdated })
  })
}

I guess the problem is because the req.body is empty (the console.log shows nothing). I hope you can help me, thank you very much!

    
asked by martiyo 09.08.2017 в 04:07
source

2 answers

0

Try the following:

function updateNota(req, res){
  let notaId = req.params.notaId
  let update = req.body
  console.log(update)

  Nota.findByIdAndUpdate(notaId, update, (err, notaUpdated) => {
    if(err) return res.status(500).send({message: 'Error al actualizar la nota: ${err}'})

    if(!notaUpdated) return res.status(500).send({message: 'No retornó objeto actualizado'})

    res.status(200).send({ nota: notaUpdated })
  })
}

Note that I added return in the if's, and that the if I added validates that the updated object returns to you. If you do not return the object, then it is not updating. By the way, as you have defined your model, when you update a document, you will return the document before updating or the updated document. The idea is that you return an object, if you do not return it is that the operation was not done.

But if it is returned to you, it means that it is updated. Enter the mongo console and do:

use 'mi-db'
db.micoleccion.find().pretty() 

to see if it is modifying you. You only replace the values according to your DB and your collection. This is to avoid having to install a robomongo or mongobooster type program to consult your DB.

As I was saying, if you do not mark any error, it is that you update the doc with the same values, you can place this at the beginning of your function:

if (!req.body.campo1) return res.status(500).send({ message: 'No se recibió valor del campo1' })

This is valid if you are receiving the values to update.

Note1 : placing the return in the errors ifs is to finish executing the function, but it will continue executing the rest of the code and will give an error of "trying to send the return headers when they have already been sent "(you can not make more than one res.status ...)

Note2 : the return http codes, for good practices, should be according to the cause of them. Code 500 is "Unknown error" and that does not give enough information. See here for more.

    
answered by 23.08.2017 / 20:43
source
0

Access the database with a little program called Robomongo to see the registry values notaUpdated returns the record that you have Modified, without the modifications made (but the registry if you have modified), if it has not left by if (err) means that it has gone well and in theory has updated the registry well.

    
answered by 09.08.2017 в 09:35