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?

1
  • 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? Commented Feb 26, 2021 at 2:46

1 Answer 1

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?

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

Comments

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.