I have an aws lambda function that is return null when it hits request. My test for the request worked until I set up the database connection. Any thoughts as to why it's not running request?
The logs show the ready state as 1 then it returns null.
***Edit
That was the first issue, once I promised it I had an issue with the database connection going away before executing my schema methods. I'll answer below.
const request = require('request')
const tickerController = require('./controllers/ticker');
const mongoose = require('mongoose');
let conn = null;
const uri = `mongodb+srv://${process.env.dbUser}:${process.env.dbPassword}@cluster0-oj6p1.mongodb.net/test?retryWrites=true&w=majority`;
exports.handler = async function main(event, context, lambdaCallback) {
// Make sure to add this so you can re-use `conn` between function calls.
// See https://www.mongodb.com/blog/post/serverless-development-with-nodejs-aws-lambda-mongodb-atlas
context.callbackWaitsForEmptyEventLoop = false;
// Because `conn` is in the global scope, Lambda may retain it between
// function calls thanks to `callbackWaitsForEmptyEventLoop`.
// This means your Lambda function doesn't have to go through the
// potentially expensive process of connecting to MongoDB every time.
if (conn == null) {
conn = await mongoose.createConnection(uri, {
// Buffering means mongoose will queue up operations if it gets
// disconnected from MongoDB and send them when it reconnects.
// With serverless, better to fail fast if not connected.
bufferCommands: false, // Disable mongoose buffering
bufferMaxEntries: 0, // and MongoDB driver buffering
useUnifiedTopology: true,
useNewUrlParser: true
});
// conn.model('Test', new mongoose.Schema({ name: String }));
// console.log(conn);
console.log(conn.readyState);
runRequest(lambdaCallback);
// })
}
};
function runRequest(lambdaCallback) {
request('https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=BKUH&apikey=' + process.env.apiKey, (err, response, body) => {
console.log('Request ran');
if (err) {
console.error(err);
done(502, '{"message", "Error retrieving ticker data"}', 'application/json', lambdaCallback);
} else {
try {
.....
donecoming from ?