I need to know how I can connect IBM Cloud Function with data base MongoDB and getting the data to show it, I try with the https://cloud.ibm.com/functions/ and I use the hello world function, I change in the code but is not working
Asked
Active
Viewed 703 times
0
-
The code for an IBM Cloud solution tutorial on a database-driven Slackbot uses Cloud Function access Db2 or PostgreSQL. See here https://github.com/IBM-Cloud/slack-chatbot-database-watson/ and adapt it to MongoDB – data_henrik Aug 08 '19 at 09:34
-
Can you paste up the sample code that isn't working for you? – James Thomas Aug 08 '19 at 14:33
1 Answers
2
Here's a good documentation of how mongoDB can be used with IBM Cloud functions: https://thecodebarbarian.com/getting-started-with-ibm-cloud-functions-and-mongodb
It has a step by step flow for getting through your requirement for NodeJS.
const mongodb = require('mongodb');
const uri = 'mongodb+srv://OMITTED.mongodb.net/test';
let client = null;
async function main(params) {
const reused = client != null;
if (client == null) {
client = await mongodb.MongoClient.connect(uri);
}
const docs = await client.db('test').collection('tests').find().toArray();
return { body: { result: docs, reused } };
}
exports.main = main;
The code above should be what you end up with.

Rohit Shetty
- 341
- 2
- 10