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

时间:2019-04-17 03:16:03

标签: mongodb mongoose aws-lambda

...
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)" ] }

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

1 个答案:

答案 0 :(得分: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,以查看其超时问题或代码执行错误。