Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

479
Visualizações
How to run callback after mongoose findOneAndUpdate finish updating?

In my API i have the express's app.put method,where i search in a collection for a document with a specific title with mongoose's findOneAndUpdate method and update it.


    app.put("/articles/:articleTitle",(req, res) => {

        Article.findOneAndUpdate({
               title: req.params.articleTitle
           }, {
               title: req.body.title,
               content: req.body.content
           }, (err, foundArticle) => {
               err ? res.send("Error !") : foundArticle ? res.send("Article found.Updating...") : res.send("NO articles found to update!")
           })

       })

How can i run a callback function AFTER the update has finished,and maybe return the updated document ? I have tried to add a .then at the end of findOneAndUpdate method but it throws me this error : "MongooseError: Query was already executed: Article.findOneAndUpdate..."

I have also tried to pass the method's return value in a variable and access it in .then but it throws the same error like so :


    app.put("/articles/:articleTitle",(req, res) => {
        
       const doc = Article.findOneAndUpdate({
               title: req.params.articleTitle
           }, {
               title: req.body.title,
               content: req.body.content
           },{returnDocument: 'after'}, (err, foundArticle) => {
               err ? res.send("Error !") : foundArticle ? res.send("Article found.Updating...") : res.send("NO articles found to update!")
           }).then( () =>{ console.log(doc) })
           
       })

EDIT : It seems that when i simply add the option returnDocument: 'after' as a parameter,it gives me access to the "after" document inside its own method callback,hmm,i have to check the docs but if anyone knows the common practice to do this i would appreciate it !


        Article.findOneAndUpdate({
            title: req.params.articleTitle
        }, {
            title: req.body.title,
            content: req.body.content
        },{returnDocument: 'after'}, (err, foundArticle) => {
            err ? res.send("Error !") : foundArticle ? res.send(foundArticle) : res.send("NO articles found to update!")
        })

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to the docs, findOneAndUpdate():

[...] finds the first document that matches a given filter, applies an update, and returns the document. By default, findOneAndUpdate() returns the document as it was before update was applied.

To actually return the updated document, the documentation suggests to do the following:

You should set the new option to true to return the document after update was applied.

The third argument to the method is an optional options object where a new parameter can be set to true in order to actually return the updated document.

To actually access the updated document after the method call, there are multiple ways of doing so.

Example 1: Using async/await syntax

const doc = await Article.findOneAndUpdate(
    { title: req.params.articleTitle },
    {
        title: req.body.title,
        content: req.body.content
    },
    { new: true }
)

doc.title // equals value of req.body.title

Example 2: Using .then() syntax

Article.findOneAndUpdate(
    { title: req.params.articleTitle },
    {
        title: req.body.title,
        content: req.body.content
    },
    { new: true }
).then((err, doc) => {
    console.log(doc.title) // equals value of req.body.title
})

Example 3: Using the callback syntax

Article.findOneAndUpdate(
    { title: req.params.articleTitle },
    {
        title: req.body.title,
        content: req.body.content
    },
    { new: true },
    (err, doc) => {
        console.log(doc.title) // equals value of req.body.title
    })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda