1

I'm trying to compile a Lambda function that uses Mongoose but nothing appears to be happening with the database? no console logs etc.. here's my current code:

index.js

const connectToDatabase = require('./db');
const Match = require('./models/Match');

exports.handler = async (event, context, callback) => {

    context.callbackWaitsForEmptyEventLoop = false;

    let player_1_name = 'test name';

    let player_1_network = 'test network';

    let match_id = 1;

    connectToDatabase().then(() => {

        var MyModel = new Match({
            player_1_name: player_1_name,
            player_1_network: player_1_network,
            match_id: match_id
        });

        MyModel.save().then(() => {
            console.log('Data saved');
        }).catch(e => {
            console.log(e);
        });

    });

});

db.js

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(process.env.DB, {useMongoClient: true}).then(db => {
    isConnected = db.connections[0].readyState;
  }).catch(e => {
    console.log('Error while DB connecting');
    console.log(e);
  });
};

models/Match.js

const mongoose = require('mongoose');

const MatchSchema = new mongoose.Schema({
    player_1_name: {
        type: String,
        required: true
    },
    player_1_network: {
        type: String,
        required: true
    },
    player_1_matches: {
        type: Number,
        default: 0
    },
    player_1_kills: {
        type: Number,
        default: 0
    },
    player_1_last_updated: {
        type: Date,
        default: null
    },
    player_2_name: {
        type: String,
        default: null
    },
    player_2_network: {
        type: String,
        default: null
    },
    player_2_matches: {
        type: Number,
        default: 0
    },
    player_2_kills: {
        type: Number,
        default: 0
    },
    player_2_last_updated: {
        type: Date,
        default: null
    },
    match_id: {
        type: Number,
        required: true
    },
    status: {
        type: Boolean
    },
});

module.exports = mongoose.model('Match', MatchSchema);

When running any tests whether it be via the API Gateway or direct Lambda tests, nothing appears to be adding to the log, all I'm getting on my log is very minimal information.

Here's a screenshot of my what I actually get in my logs: enter image description here

1 Answer 1

1

Notice the line that says context.callbackWaitsForEmptyEventLoop = false;. This false flag means the following: whenever lambda finishes execution of your handler function, it'll terminate the process immediately, even if there is something left in the event loop to process. And in your case, your handler function makes a request to mongodb with a promise, but you do not await that promise. You just spin it up and let it work in the background, but due to that false flag I mentioned earlier, lambda will terminate the process immediately, even though it sees that there's network I/O to process in the event loop. That's why you don't even see any logs - then or catch statements of the promise are not even executed.

So, if you are querying mongodb in the background for purpose, I recommend you to remove that false flag. If you don't need to do that in the background, add await to your mongodb query.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, I've attempted to add await to my function but now my function just hands indefinitely?
it's most likely a different issue. Is your mongodb in a private subnet?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.