简体   繁体   中英

Lambda NodeJS not inserting into MongoDB

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: 在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM