English 中文(简体)
AWS Lambda Node JS Promise Rejection
原标题:

I m having an issue with one of my AWS Lambda Node JS functions when I call the function from axios in my Next JS frontend. The first time the function is called, it is successful and returns correctly but when I call it again, it returns Error: Runtime exited with error: exit status 128 and ERROR Unhandled Promise Rejection Runtime.UnhandledPromiseRejection . When I call it again after a failed attempt, it runs successfully and returns correctly. Is there an issue with my code (below)? I ve got context.callbackWaitsForEmptyEventLoop set to false but I can t see why the function fails with rapid fire calls. Thanks in advance

const MongoClient = require("mongodb").MongoClient;
const ObjectId = require("mongodb").ObjectId;

const MONGODB_URI = process.env.MONGODB_URI;

let cachedDb = null;

async function connectToDatabase() {
  if (cachedDb) {
    return cachedDb;
  }

  const client = await MongoClient.connect(MONGODB_URI);

  const db = await client.db(process.env.MONGODB_NAME);

  cachedDb = db;
  return db;
}

exports.handler = async (event, context) => {
  console.log(event.queryStringParameters);
  console.log(event.body);
  let docType = event.queryStringParameters.docType;
  let docId = event.queryStringParameters.docId;
  const bodyData = JSON.parse(event.body);
  let fundId = event.queryStringParameters.fundId;

  context.callbackWaitsForEmptyEventLoop = false;

  const db = await connectToDatabase();

  try {
  const data = await db.collection(docType).findOneAndUpdate({_id : ObjectId(docId), "profile_subscription_documents.fund_id" : fundId},{
    $push: {"profile_subscription_documents.$[psd].profile_fund_subscription_documents": {...bodyData} }
  },
  {
    arrayFilters: [
      {
        "psd.fund_id": fundId
      }]
  },
  {
    returnDocument: "after"
  });
  
  console.log("Here", data);

  const response = {
    statusCode: 200,
        headers: {
            "Access-Control-Allow-Headers" : "*",
            "Access-Control-Allow-Origin": "http://localhost:3000/",
            "Access-Control-Allow-Methods": "*"
        },
    body: JSON.stringify(data),
  };

  return response;
  } catch (error) {
    console.error(error);
  }
  
};
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签