【问题标题】:Lambda NodeJS not inserting into MongoDBLambda NodeJS 未插入 MongoDB
【发布时间】:2019-08-03 22:53:48
【问题描述】:

我正在尝试编译一个使用 Mongoose 的 Lambda 函数,但数据库似乎没有发生任何事情?没有控制台日志等。这是我当前的代码:

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);

在运行任何测试时,无论是通过 API 网关还是直接 Lambda 测试,似乎都没有添加到日志中,我在日志中得到的只是非常少的信息。

这是我在日志中实际获得的内容的屏幕截图:

【问题讨论】:

    标签: javascript node.js mongodb mongoose aws-lambda


    【解决方案1】:

    注意context.callbackWaitsForEmptyEventLoop = false; 的行。这个 false 标志的含义如下:每当 lambda 完成处理函数的执行时,它会立即终止进程,即使事件循环中还有一些东西要处理。在您的情况下,您的处理程序函数向 mongodb 发出一个带有承诺的请求,但您没有 await 那个承诺。您只需启动它并让它在后台工作,但是由于我前面提到的那个错误标志,lambda 将立即终止该进程,即使它看到在事件循环中存在要处理的网络 I/O。这就是为什么你甚至看不到任何日志 - then 或 catch 承诺的语句甚至没有被执行。

    因此,如果您是出于目的在后台查询 mongodb,我建议您删除该错误标志。如果您不需要在后台执行此操作,请将 await 添加到您的 mongodb 查询中。

    【讨论】:

    • 谢谢你,我试图将await 添加到我的函数中,但现在我的函数只能无限期地使用?
    • 这很可能是另一个问题。您的 mongodb 在私有子网中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2018-03-25
    • 2021-03-12
    • 1970-01-01
    • 2020-04-15
    相关资源
    最近更新 更多