0
...
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

There is no error in holiq.js event output correctly and mongodb connect successfully but

error: End - Error:
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)" ] }

time out error occured Please help me!~

1 Answer 1

1

I think only one $set needs to be used for updating

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

Increase lambda execution time to 10 secs to see if its time out problem or code execution error.

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

5 Comments

Thank you Amol B Jamkar. Another error occur now. ...node_modules/mongodb/lib/utils.js:123 process.nextTick(function() { throw err; }); ^ AssertionError [ERR_ASSERTION]: 1 == 0
I think mongo cannot find a to update the document. Refer this link stackoverflow.com/questions/42205665/…, Please check your database first.
I checked my database and there is column that I finding.
upadateOne returns { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } as per reference document docs.mongodb.com/manual/reference/method/…. In your case check should be assert.equal(1, result.result.modifiedCount); or assert.equal(1, result.modifiedCount);
In my case it returns modifiedCount : 0 So I use mongoose and mongoose works well. Anyway, Thanks for your help Amol B Jamkar

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.