Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
Why is the value of this returned promise not updating?

I have a program where, upon clicking a button, the "likes" attribute of a randomly fetched MongoDB document is supposed to update. However, it does not. I am able to call the fetched document through the button, not not actually update it:

MongoClient.connect(DBconnection, { useUnifiedTopology: true })
  .then(client => {
    const db = client.db(database);
    const collection = db.collection(table);

    //R has been previously defined as a random number
    let doc = function(data) {
      return db.collection(table).find().limit(-1).skip(R - 1).next()
        .then(results => { return results })
    }
    
    //This is supposed to update the value of the randomly fetched document
    app.post("/", (req, res) => {
      let w = doc()
      w.then(function(results) {
        console.log(results) //This correctly returns the random document
      })
      //This line is meant to update document "w", but it does not.
      db.collection(table).updateOne({ w }, { $set: { likes: "clicked" + R } })
        .then(result => {
          res.redirect("/")
        })
        .catch(error => console.error(error))
    });
  });

The button in the ejs file is as simple as:

<form action="/" method="POST">
    <button id="updoot" type="submit">upvote</button>
</form>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Check the status of the promise by console logging (result). If it returns a pending promise, try async-await which will resolve the promise, and then executes res.redirect.

app.post("/", async (req, res) => {
      let w = await doc()
      const updatedData = await db.collection(table).updateOne({ w }, { $set: { likes: "clicked" + R } })
      res.redirect("/")
  });

I think it should work.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Alright, thanks to the contribution of jkalandarov, I was able to solve it just by adding one extra step: requesting the ObjectId of w and using that as a filter instead of the returned promise of w:

app.post("/", async (req, res) => {
      let w = await doc()
      var oid = w._id
      let updatedData = await db.collection(table).updateOne({"_id":ObjectId(oid)}, { $set: { likes: "clicked" + R } })

      res.redirect("/")
  });

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda