I'm creating a connection for my MongoDB Atlas using NodeJS in a AWS Lambda function. I was able to make a successful connection, but when I do the .find({}) to get all data I get an empty array.
Lambda
const mongoose = require('mongoose');
let conn = null;
const uri = 'mongodb+srv://xxx:[email protected]/test';
let M = null;
exports.handler = async function(event, context) {
nodejs-aws-lambda-mongodb-atlas
context.callbackWaitsForEmptyEventLoop = false;
if (conn == null) {
conn = await mongoose.createConnection(uri, {
bufferCommands: false,
bufferMaxEntries: 0,
useNewUrlParser: true
});
M = conn.model('Todo', new mongoose.Schema(
{
title: String,
description: String,
date: Date,
status: String
}
));
}
const doc = M.find({});
console.log('items in the DB')
console.log(doc);
return null;
};
Documentation Mongoose AWS Lambda: https://mongoosejs.com/docs/lambda.html
UPDATE:
M.find({}).exec().then(function(allDocs){
console.log('getting al docs')
console.log(allDocs)//[]
});
MongoDB Atlas

mongoose.modelinstead ofconn.model