0

I want to make an openwhisk action that does something really simple: performs a find query in a mongodb and returns the result. However, since I am new in those technologies, I cannot find a solution, cause I am always recieving no results. I have connected openwhisk with the mongodb correctly. Can anyone help with a code example?

My code so far is this:

function main(){
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://192.168.1.14:27017/'

    MongoClient.connect(url, (err, db) => {
        db.db('yelp').collection('Review').find({stars:5}).limit(100).toArray().then((docs) => {
            return docs;
            db.close();            
        }).catch((err) => {
            console.log(err.stack);
        });
    })
}

I am recieving null as a result. Any suggestions?

zak
  • 95
  • 1
  • 7
  • I'm facing the same kind of trouble as yours. Did you find a solution? ...the answer below doesn't help much. I know I can return a promise. But my question is, does openwhisk deal with promises? Will it return the content of what gets resolved in that promise? – steoiatsl Feb 26 '21 at 02:46

1 Answers1

1

You need to use something like Promises or async/await to deal with async, and wait until the respond is back from the DB to end the action execution

See this question on how to use promises with mongodb client in nodejs How to use MongoDB with promises in Node.js?

csantanapr
  • 5,002
  • 2
  • 19
  • 15