0
I am doing . map() in an array, to do a Mongoose/Mongodb query with each element of this array and returns another array with the results.
However, after this process, I try to access the new array with console.log(newArray) and what I print is [Object Promise], and I wanted the results of the queries that should be in this array. NOTE: The queries worked (tested with 1): each one is saved in the value variable. See below:
async function buscarNoBdPorValorContido(model, atributo, valor, resposta){
var documentos = []
var valores = valor.split(` `)
documentos = await valores.map(async valor => {
const query = model.find({url: {'$in': [new RegExp(`.*${valor}.*`, 'i')]}})
let value = await query.exec() //Tudo certo aqui
return value
})
console.log('resultado '+documentos) //Imprime "resultado [object Promise]"
}
History summary, I want to use the document array whose contents should be the results of queries. I’ve been at it for hours, I don’t know what else to try. Thank you!
Could you give an example? I don’t think your explanation is clear to someone who isn’t very familiar with.
– Andre
One moment personal, I’ll test.
– Lucas Pletsch
@Rodrigoazevedo Could you show me?
– Lucas Pletsch
@Rodrigoazevedo But each query was being made inside map in Let value = await query.exec(), so do I get this snippet? I simply give Return query inside the map?
– Lucas Pletsch
@Rodrigoazevedo what would await look like? Is that I want to use the solution of the Promises still within this function, to be able to return these values out of it
– Lucas Pletsch