简体   繁体   English

INSERT INTO TABLE 查询在nodejs Lambda函数内没有响应

[英]INSERT INTO TABLE query gives no response inside nodejs Lambda function

I'm using lambda functions to do my database operations.我正在使用 lambda 函数来执行我的数据库操作。 When i perform GET from my client side, SELECT statements work fine meaning it can connect and retrieve data.当我从客户端执行 GET 时,SELECT 语句工作正常,这意味着它可以连接和检索数据。 However, I cant get this insert statement to work.但是,我不能让这个插入语句工作。

My code:我的代码:

try {
  conn = await oracledb.getConnection(connAttr);

  console.log('Connected to database');

  let result = await conn.execute(
    "INSERT INTO product (NAME, DISCOUNT, DATE_VALID_FROM, DATE_VALID_TO) VALUES (:NAME, :DISCOUNT, TO_DATE(:DATE_VALID_FROM, 'DD-MM-YYYY'), TO_DATE(:DATE_VALID_TO, 'DD-MM-YYYY'))",
    {
      NAME: requestBody.Name,
      DISCOUNT: requestBody.Disc,
      DATE_VALID_FROM: requestBody.StartDate,
      DATE_VALID_TO: requestBody.EndDate,
    },
    function (err, result) {
      const response = {
        statusCode: 200,
        body: JSON.stringify(result),
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Headers': '*',
          'Access-Control-Allow-Methods': '*',
          'Access-Control-Allow-Credentials': 'true',
          'content-type': 'application/json',
        },
      };
      callback(null, response);
      if (err) {
        console.log('error is here');
      }
    }
  );
} catch (err) {
  const response = {
    statusCode: 400,
    body: JSON.stringify({ message: err }),
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': '*',
      'Access-Control-Allow-Methods': '*',
      'Access-Control-Allow-Credentials': 'true',
      'content-type': 'application/json',
    },
  };
}

when i call this from the client side form I get Http Error 502 and when I view in cloudwatch logs I can see 'Connected to database' and then 'error is here' which means the query is never executed successfully.当我从客户端表单调用它时,我得到Http Error 502 ,当我在 cloudwatch 日志中查看时,我可以看到'Connected to database' and then 'error is here' ,这意味着查询永远不会成功执行。

I'm using a lambda layer with the package oracledb-prebuilt-for-lambda which is basically node-oracledb.我正在使用带有 oracledb-prebuilt-for-lambda 包的 lambda 层,它基本上是 node-oracledb。

When i console log requestbody i can see the parameters I passed to the lambda.当我控制台记录 requestbody 时,我可以看到我传递给 lambda 的参数。

I have a POST method with proxy lambda integration to this lambda and CORS is enabled as well.我有一个将代理 lambda 集成到此 lambda 的 POST 方法,并且也启用了 CORS。

It seems that only the query that is not working.似乎只有不工作的查询。 Any idea what I am doing wrong?知道我做错了什么吗?

You are mixing async/await and callback styles of programming.您正在混合 async/await 和回调编程风格。 Just use async/await.只需使用异步/等待。 For example your call to the SQL statement should simply be:例如,您对 SQL 语句的调用应该是:

  let result = await conn.execute(
    "INSERT INTO product (NAME, DISCOUNT, DATE_VALID_FROM, DATE_VALID_TO) VALUES (:NAME, :DISCOUNT, TO_DATE(:DATE_VALID_FROM, 'DD-MM-YYYY'), TO_DATE(:DATE_VALID_TO, 'DD-MM-YYYY'))",
    {
      NAME: requestBody.Name,
      DISCOUNT: requestBody.Disc,
      DATE_VALID_FROM: requestBody.StartDate,
      DATE_VALID_TO: requestBody.EndDate,
    })

After that you do result handling之后你做结果处理

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

相关问题 在 nodejs 中的 lambda function 中的 AWS IAM 授权 - AWS IAM authorization inside a lambda function in nodejs nodejs Lambda函数内部的FFMPEG意外行为 - FFMPEG unexpected behavior inside nodejs Lambda function Alexa,nodejs使用Lambda通过不同的参数多次查询DynamoDB表。 异步功能 - Alexa, nodejs using Lambda to query DynamoDB table multiple times with different parameters. Asynchronous function AWS Lambda nodejs mysql 循环内查询不起作用 - AWS Lambda nodejs mysql query inside loop is not working 如何使用nodejs在aws lambda函数中调用rest api - how to call rest api inside aws lambda function using nodejs nodejs在变量中存储function的返回值给我未定义 - nodejs storing the returned value of a function inside a variable gives me undefined 查询未在循环函数 nodejs MySQL 内执行 - Query is not executed inside of a loop function nodejs MySQL 如何从 NodeJs 中 function 中的 function 响应? - How to response from a function inside a post function in NodeJs? 如何使用Node.js和Mocha在另一个Lambda函数中模拟AWS Lambda函数调用 - How to mock an aws lambda function call inside another lambda function with nodejs and mocha AWS Lambda函数使用nodejs来ssh到ec2并运行命令,在测试中获得200次成功但不执行任何操作 - AWS Lambda function with nodejs to ssh to ec2 and run command, on testing gives 200 success but does nothing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM