Nodejs等待不等待

I have a simple MongoDbB example using await, but for some reason the code doesn't wait when I use the await keyword...

index.js:

require('dotenv').config()
const utilFunctions = require('./functions')

const getKeywords = utilFunctions.getKeywords

const main = async () => {

    const keywords = await getKeywords(process.env.MONGO_URI)

    console.log('keywords: ', keywords)

}

main()

and functions.js:

var MongoClient = require('mongodb').MongoClient;

const getKeywords = async (uri) => {
    console.log('uri is: ', uri)

    MongoClient.connect(uri, function (err, db) {
        if (err) throw err;

        console.log('connected...')

        var dbo = db.db("eon-data")

        dbo.collection("twitter-keyword-scanner").find({}).toArray(function (err, mainDoc) {
            if (err) throw err;

            const keywords = mainDoc[0].config.keywordsToLookFor
            console.log('got keywords: ', keywords)
            db.close()
            return keywords
        })
    })
}


module.exports = {
    getKeywords
}

When I run node index.js I get this output:

uri is:  (my mongo uri)
(node:23990) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
keywords:  undefined
connected...
got keywords.  [
  (data in the db)
]

由于某种原因,异步/等待无法正常工作...

它确实的确在日志中显示了“ got keyword:”中的关键字,但是在index.js中,它正在打印“ keywords:undefined”,并在“ getKeywords”返回之前将其打印出来。

I would expect the console.log to happen after the function getKeywords returns, but actually it is being run beforehand. Am I doing something wrong here? Can anyone see why the async/await is not working properly?

谢谢!

PS- you can find the full project here: https://github.com/JimLynchCodes/Ameritrader-Bots/tree/master/twitter-keyword-scanner

在节点v12.16.1上运行