0

我需要知道如何将 IBM Cloud Function 与数据库 MongoDB 连接并获取数据以显示它,我尝试使用https://cloud.ibm.com/functions/并使用 hello world 函数,我改变了代码但不工作

4

1 回答 1

2

以下是有关如何将 mongoDB 与 IBM Cloud 函数一起使用的良好文档:https ://thecodebarbarian.com/getting-started-with-ibm-cloud-functions-and-mongodb

它有一个逐步的流程来满足您对 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;

上面的代码应该是你最终得到的。

于 2019-08-08T15:32:48.753 回答