1

I am using mongo to search for all entries with a value that is like whatever is passed into the searchForAsset method. When I use the .toArray method and I console.log(result) the array of files is printed in array format. However when I return that exact same variable below the console.log and I console.log it in the .then() below, it comes back as "result: undefined". Why doesn't the .then() wait for the result to be returned correctly?

async function searchForAsset(nameToQuery){
    const client = await mongoConnect()
    client.db(process.env.DATABASE_NAME).collection(process.env.ASSET_COLLECTION).find({asset_name: RegExp(nameToQuery)}).toArray(function(err, result) {
        if (err) throw err
        console.log(result)
        client.close()
        return result
    })
}

searchForAsset("Solution").then(result => {
    console.log("result: "+result)
})

Edit: I changed the code to this and it still returns undefined:

async function searchForAsset(nameToQuery){
    const client = await mongoConnect()
    const result = await client.db(process.env.DATABASE_NAME).collection(process.env.ASSET_COLLECTION).find({asset_name: RegExp(nameToQuery)}).toArray(function(err, result) {
        if (err) throw err
        console.log(result)
        client.close()
    })
    return result
}

searchForAsset("Solution").then(result => {
    console.log("result: "+result)
})
7
  • 1
    Why aren't you using await in an async function? const result = await client.db(process.env.DATABASE_NAME).collection(process.env.ASSET_COLLECTION).find({asset_name: RegExp(nameToQuery)}).toArray() or you need to add return before client.db(...) Commented Jul 22, 2021 at 15:54
  • 1
    searchForAsset is not returning anything. If there's no return statement in a function, you will get undefined Commented Jul 22, 2021 at 15:57
  • @Dharmaraj @Evert if I change the method to const result = await client.db(process.env.DATABASE_NAME).collection(process.env.ASSET_COLLECTION).find({asset_name: RegExp(nameToQuery)}).toArray(function(err, result) {}) and then after that do return result it still results in an undefined console.log() in the .then() Commented Jul 22, 2021 at 16:28
  • You don't use call back with await generally. Try copying code from this gist Commented Jul 22, 2021 at 16:30
  • @Dharmaraj then the return looks like this: result: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]... and so on Commented Jul 22, 2021 at 16:33

1 Answer 1

1

Try using await as the function is async like this:

async function searchForAsset(nameToQuery){
    const client = await mongoConnect()
    const result = await client.db(process.env.DATABASE_NAME).collection(process.env.ASSET_COLLECTION).find({asset_name: RegExp(nameToQuery)}).toArray()
    return result 
}

searchForAsset("Solution").then(result => {
    console.log("result: "+result)
})

About that [object Object] being logged. Try stringifying that:

console.log(JSON.stringify(result))

Then to view it in a prettier way, copy the log output and paste it in https://jsonformatter.org/

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

2 Comments

yes the console.log(JSON.stringify(result)) works but for large outputs its just a wall of text, how can I turn this back into a json that looks nice
@RileyEaton like view it yourself or in your app? Try using the JSON Formatter

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.