2

I'm doing mongoose queries using as parameter the values of an array. So I need to do many queries. And I want to save them in an object out of the promise for to use it later in the same function.

But, the code below just prints "[undefined]". However, I know that the first querie returns a value, because I've tested it alone before, so the array with undefined that returns is wrong. I can't even imagine now what I'm doing wrong.

 async function buscarNoBdPorValorContido(model, atributo, valor, resposta){
        var documentos = []
        var valores = valor.split(` `)
        documentos = await Promise.all(valores.map(async valor => {
                const query = model.find({url: {'$in': [new RegExp(`.*${valor}.*`, 'i')]}}) //Everything working here

            }))

            console.log(documentos) //Prints " [undefined] "       
 }
Barthy
  • 3,151
  • 1
  • 25
  • 42
Lucas
  • 157
  • 1
  • 12
  • 3
    The promises in `Promise.all` don't return anything. You just create a variable and that's it - it's not used any more. You probably just need `return model.find({url: {'$in': [new RegExp(`.*${valor}.*`, 'i')]}})` instead of declaring a variable. – VLAZ Jun 13 '20 at 23:10
  • 1
    As written, the map callback does not return anything; it needs to return Promise. `query` is not a Promise but `query.exec()` returns Promise, therefore you need to write `return model.find(.....).exec();` – Roamer-1888 Jun 13 '20 at 23:31
  • 1
    `valores.map(...)` will thus return an array of promises, which is exactly what `Promise.all()` needs. If all the queries are successful, `documentos` will contain the results of the queries. If any one of the queries fails, the whole `Promise.all()` will fail, so you need a `try{} catch(error){}` structure to catch and handle the possibility of an error. – Roamer-1888 Jun 13 '20 at 23:36
  • 1
    Duplicate of "How do I access previous promise results in a .then() chain?" - balderdash! – Roamer-1888 Jun 14 '20 at 00:00
  • you just gotta return inside the arrow function – Tibebes. M Jun 14 '20 at 08:52

2 Answers2

1

try returning the query in the async function inside map.

 async function buscarNoBdPorValorContido(model, atributo, valor, resposta){
        var documentos = []
        var valores = valor.split(` `)
        documentos = await Promise.all(valores.map(async valor => {
                return model.find({url: {'$in': [new RegExp(`.*${valor}.*`, 'i')]}}) //Everything working here

            }))

            console.log(documentos) //Prints " [undefined] "       
 }
Karambit
  • 317
  • 4
  • 11
0

I've solved like this:

 async buscarNoBdPorValorContido(model, atributo, valor){
        var valores = valor.split(` `)
        var documentos = valores.map(value => {
            const query = model.find({url: {'$in': [new RegExp(`.*${value}.*`, 'i')]}})
            return query.exec() 
        })
        var resultados = await Promise.all(documentos)
        return resultados  

    },
Lucas
  • 157
  • 1
  • 12