我正在使用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