1

I have tried a lot and didn't get anything that works good for me. I tried with promise.all and setting the array globally, but with no success.

I want to search in three collections on MongoDB and when the find matches, set an object with the infos and push to an array. At the end, send a response with the objects array.

router.post('/certificado', (req, res) => {
  let cpf = splita(req.body.cpf)
  let array = []

  function pesquisaProjeto(cpf) {
    return new Promise(function (fulfill, reject) {
      ProjetoSchema.find({'integrantes.cpf':cpf}, 'integrantes.$ nomeProjeto -_id',(err, usr) => {
        if (err) return reject(err)
        fulfill(usr)
      });
    })
  }

  function pesquisaAvaliador(cpf) {
    return new Promise(function (fulfill, reject) {
      avaliadorSchema.find({'cpf':cpf}, 'nome -_id',(err, usr) => {
        if (err) return reject(err)
        fulfill(usr)
      })
    })
  }

  function pesquisaParticipante(cpf) {
    return new Promise(function (fulfill, reject) {
      participanteSchema.find({'cpf':cpf}, 'nome eventos -_id', (err, usr) => {
        if (err) return reject(err)
        fulfill(usr)
      })
    })
  }

  pesquisaProjeto(cpf)
  .then(usr => {
    let participante = ({
      tipo: usr[0].integrantes[0].tipo,
      nome: usr[0].integrantes[0].nome
    })
    array.push(participante)
    console.log(participante)
  })
  .catch(err => console.log("Não encontrou nada nos projetos. " + err.message))

  pesquisaAvaliador(cpf)
  .then(usr => {
    let participante = {
      tipo: "Avaliador",
      nome: usr[0].nome
    }
    array.push(participante)
    console.log(array)
  })
  .catch(err => console.log("Não encontrou nada nos avaliadores. " + err.message))

  pesquisaParticipante(cpf)
  .then(usr => {
    let participante = ({
      tipo: "Participante",
      nome: usr[0].nome,
      eventos: usr[0].eventos
    })
    array.push(participante)
    console.log(array)
  })
  .catch(err => console.log("Não encontrou nada nos participantes dos eventos. " + err.message))

    **Anything like res.send(array) that I was tired to try**
});

Sorry for the stupid doubt, but I've spent so much time trying to find the solution that I decided to resort to the community.

Thanks!

4
  • why didn't promise.all work? Commented Nov 21, 2016 at 17:52
  • Don't know. When I try to res.send(array) at the .then() of promise.all nothing is sended to front. Think that could be some wrong with the push inside the promise and the array declaration. Commented Nov 21, 2016 at 18:08
  • Please show us how you tried to use Promise.all. Commented Nov 21, 2016 at 19:02
  • @RicardoSwarovsky add your solution as an answer, not an edit to your question. Commented Nov 22, 2016 at 23:24

1 Answer 1

1

If I understand your question right you have multiple promises and want to wait for all of them to finish. You can do that with Promise.all().

If one Promise will fail Promise.all() will also fail. But if you catch them like you do in your example and return nothing I think the Array should be populated with undefined for that Query. So you could then filter those empty values out if you want to.

const one = dbQueryOne.then(usr => ({
  key: usr.val
}))
.catch(err => { console.log(err) })

const two = dbQueryTwo.then(usr => ({
  key: usr.val
}))
.catch(err => { console.log(err) })

const three = dbQueryThree.then(usr => ({
  key: usr.val
}))
.catch(err => { console.log(err) })

Promise.all([one, two, three]).then(arr => {
  res.send(arr.filter(val => val !== undefined ))
})

usr => ({ key: val }) is just short for usr => { return { key: val } }

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This really solved the problem!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.