AWS Lambda和mongo中的任务超时错误

问题描述 投票:1回答:1
...
exports.handler = function(event, context, callback) {
  context.callbackWaitsForEmptyEventLoop = false; //keeps mongoose connection active
  /*
        init values
    */
  var a = event.a,
    b = event.b,
    c = event.c,
    d = event.d,
    e = event.e;

  var output = {
    //TODO look into SSMS
    dialogAction: {
      type: "Close",
      fulfillmentState: "Fulfilled",
      message: {
        contentType: "PlainText",
        content: "Record for " + a + " successfully updated."
      }
    }
  }; //output
  // Use connect method to connect to the server
  console.log(event);
  MongoClient.connect(url, function(err, client) {
    assert.equal(null, err);
    console.log("Connected successfully to server");

    const db = client.db(dbName);
    const collection = db.collection("a");
    collection.updateOne(
      { a: a },
      { $set: { b: b } },
      { $set: { c: c } },
      { $set: { d: d } },
      { $set: { e: e } },
      function(err, result) {
        assert.equal(err, null);
        assert.equal(1, result.result.n);
        console.log("Updated the document for " + a);
        callback(result);
      }
    );
  });
};
...
lambda-local -l index.js -h handler -e holiq.js

holiq.js事件输出中没有错误,mongodb连接成功但是

错误:结束 - 错误: error: { "errorMessage": "Task timed out after 5.00 seconds", "errorType": "TimeoutError", "stackTrace": [ "Context.<anonymous> (/usr/local/lib/node_modules/lambda-local/lib/context.js:127:9)", "ontimeout (timers.js:436:11)", "tryOnTimeout (timers.js:300:5)", "listOnTimeout (timers.js:263:5)", "Timer.processTimers (timers.js:223:10)" ] }

发生超时错误请帮帮我!〜

mongodb mongoose aws-lambda
1个回答
1
投票

我认为只需要使用一个$set进行更新

collection.updateOne(
      { a: a },
      { $set: { b: b,e: e, c: c, d: d } },
      function(err, result) {
        assert.equal(err, null);
        assert.equal(1, result.modifiedCount)
        console.log("Updated the document for " + a);
        callback(result);
      }
    );

将lambda执行时间增加到10 secs以查看其超时问题或代码执行错误。

© www.soinside.com 2019 - 2024. All rights reserved.