简体   繁体   English

代码在本地工作但不在 AWS 上工作 Lambda

[英]Code working on local but not on AWS Lambda

I have written a small piece of code in NodeJS to update a MongoDB collection.我在 NodeJS 中编写了一小段代码来更新 MongoDB 集合。 The code works fine when I run it on my local machine.当我在本地机器上运行代码时,代码工作正常。 But once deployed to AWS it doesn't但是一旦部署到 AWS 它就不会

exports.handler = async (event) => {
  console.log("I am here");

  const { MongoClient } = require("mongodb");
  const uri =
    "xxxxxxx";
  const client = new MongoClient(uri);

  async function run() {
    console.log("Inside the function");
    try {
      const database = client.db("mydb");
      const movies = database.collection("TACountryMapping");
      // create a filter for a movie to update
      var filter = { Key: "Value" };
      // this option instructs the method to create a document if no documents match the filter
      const options = { upsert: true };
      // create a document that sets the plot of the movie
      const updateDoc = {
        $set: { Key: "updatedValue", address: "Canyon 123" },
      };
      const result = await movies.updateOne(filter, updateDoc, options);

      console.log(result);
      console.log(
        `${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`
      );
    } finally {
      await client.close();
    }
  }

  run().catch(console.dir);
  return 0;
};

//exports.handler();

The result object just doesn't seem to be getting created in lambda.结果 object 似乎并未在 lambda 中创建。

Following are the logs when running the code from my local machine:以下是从我的本地计算机运行代码时的日志:

I am here
Inside the function
{
  acknowledged: true,
  modifiedCount: 1,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}
1 document(s) matched the filter, updated 1 document(s)

And when run from Lambda, the following are the logs:当从 Lambda 运行时,以下是日志:

START RequestId: 3cdc0764-7238-4218-bdf2-954556f10c77 Version: $LATEST
2022-12-04T03:18:56.383Z 3cdc0764-7238-4218-bdf2-954556f10c77 INFO  I am here
2022-12-04T03:18:56.505Z 3cdc0764-7238-4218-bdf2-954556f10c77 INFO  Inside the function
END RequestId: 3cdc0764-7238-4218-bdf2-954556f10c77
REPORT RequestId: 3cdc0764-7238-4218-bdf2-954556f10c77  Duration: 478.33 ms Billed Duration: 479 ms Memory Size: 128 MB Max Memory Used: 78 MB  

I have tried to remove the await from await movies.updateOne(filter, updateDoc, options);我试图从await movies.updateOne(filter, updateDoc, options);中删除await , but that just skips the code entirely and doesn't work , 但这只是完全跳过代码并且不起作用

I think it's because you are awaiting for the movies.updateOne statement but not awaiting for the entire run() function to complete before ending the execution of the lambda.我认为这是因为您正在等待movies.updateOne语句,而不是等待整个run() function 在结束 lambda 的执行之前完成。

When a Lambda function doesn't wait for the event loop to be empty and a container is re-used, the event loop will be "paused" until the next invocation occurs.当 Lambda function 不等待事件循环为空并重新使用容器时,事件循环将“暂停”,直到下一次调用发生。

Try refactoring your code to what is shown below.尝试将您的代码重构为如下所示。 Note the use of await run() statement that will make sure the entire code has completed execution before ending execution.请注意await run()语句的使用,该语句将确保整个代码在结束执行之前已完成执行。

exports.handler = async (event) => {
  console.log("I am here");

  const { MongoClient } = require("mongodb");
  const uri ="xxxxxxx";
  const client = new MongoClient(uri);

  await run();
  return 0;
};

async function run() {
  console.log("Inside the function");
  try {
    const database = client.db("mydb");
    const movies = database.collection("TACountryMapping");
    // create a filter for a movie to update
    var filter = { Key: "Value" };
    // this option instructs the method to create a document if no documents match the filter
    const options = { upsert: true };
    // create a document that sets the plot of the movie
    const updateDoc = {
      $set: { Key: "updatedValue", address: "Canyon 123" },
    };
    const result = await movies.updateOne(filter, updateDoc, options);

    console.log(result);
    console.log(
      `${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`
    );
  }
  catch(e){
    console.error(e);
  }
  finally {
    await client.close();
  }
}

PS: I'm assuming your connection to the database is working fine here and no connection errors were noticed in AWS Cloudwatch . PS:我假设您与数据库的连接在这里工作正常,并且在AWS Cloudwatch中没有发现连接错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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