api网关节点js网关无法连接数据库mongodb地图集

时间:2019-04-07 16:14:14

标签: node.js mongodb rest api

我正在使用AWS网关的无服务器后端,但是在连接到MongoDB Atlas时邮递员出现内部错误

https://hackernoon.com/building-a-serverless-rest-api-with-node-js-and-mongodb-2e0ed0638f47 使用以下教程

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
let isConnected;

module.exports = connectToDatabase = () => {
  if (isConnected) {
    console.log('=> using existing database connection');
    return Promise.resolve();
  }

  console.log('=> using new database connection');
  return mongoose.connect('mongodb+srv://gaurav45@cluster0-ycxzk.mongodb.net/test?retryWrites=true',
{
    useNewUrlParser: true
}
)
    .then(db => { 
      isConnected = db.connections[0].readyState;
    });
};


connecting to the database to post requests

0 个答案:

没有答案
相关问题