问题

I am trying to pass an IF statement (IF data exists in table, [true] update data, [false] insert data) in NodeJS for either of the following queries:

var mysql = require('mysql');

var config = require('./config.json');

var pool = mysql.createPool({

host : config.dbhost,

user : config.dbuser,

password : config.dbpassword,

database : config.dbname

});

exports.handler = (event, context, callback) => {

context.callbackWaitsForEmptyEventLoop = false;

pool.getConnection(function(err, connection) {

// IF STATEMENT

var id = ... //select statement to the same table...??

If (id = ?) {

// If TRUE

connection.query('UPDATE table SET email = "'johndoe@icloud.com'" WHERE id = 1;', function (error, results, fields) {

// And done with the connection.

connection.release();

// Handle error after the release.

if (error) callback(error) ;

else callback(null, results);,

// If FALSE

connection.query('INSERT INTO table (id, name, email) VALUES (1, "'John Doe'", "'johndoe@gmail.com'");', event['i'], function (error, results, fields) {

// And done with the connection.

connection.release();

// Handle error after the release.

if (error) callback(error) ;

else callback(null, results);

};

});

});

};

I am new to NodeJS and I am still trying to figure out the IF STATEMENT with the codes I have above. I will keep you posted.

@CHRISWILLIAMS RESULTS DETAILS FOR HIS ANSWER

Basically, I have 2 APIs.

The first API runs initially, drop canvass_prices if exists. This one I don't have any issue. Here's the result of the first API:

Response:

[

{

"ID": 1,

"Item": "Earth Science Deep Conditioning Masque For Hair - 2 Fl Oz",

"Qty": 30,

"Container": "Bottle",

"Size": "750ml",

"Reiciendis eos nostrum ut sequi.": 1680,

"Sed quidem aspernatur quisquam ut.": 19920,

"Aut dolorem repellendus iste nisi...": 79170

}

]

Request ID:

"e2bbe38b-8121-4cb5-aa88-ac570b066759"

Function logs:

START RequestId: e2bbe38b-8121-4cb5-aa88-ac570b066759 Version: $LATEST

END RequestId: e2bbe38b-8121-4cb5-aa88-ac570b066759

REPORT RequestId: e2bbe38b-8121-4cb5-aa88-ac570b066759 Duration: 287.67 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 74 MB

And this is the 2nd API (see my 1st update here) which I am having trouble with. This API will update the canvass_prices created by 1st API.

However, the first time I clicked the result is this:

Response:

[

{

"ID": 1,

"Item": "Earth Science Deep Conditioning Masque For Hair - 2 Fl Oz",

"Qty": 30,

"Container": "Bottle",

"Size": "750ml",

"Reiciendis eos nostrum ut sequi.": 1680,

"Sed quidem aspernatur quisquam ut.": 19920,

"Aut dolorem repellendus iste nisi...": 79170,

"Voluptates laudantium voluptas nam.": null,

"Ipsum voluptatem dolorum commodi.": null

}

]

Request ID:

"4aed4c97-8ef6-485c-93b4-ca2eaf95703d"

Function logs:

START RequestId: 4aed4c97-8ef6-485c-93b4-ca2eaf95703d Version: $LATEST

END RequestId: 4aed4c97-8ef6-485c-93b4-ca2eaf95703d

REPORT RequestId: 4aed4c97-8ef6-485c-93b4-ca2eaf95703d Duration: 393.79 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 75 MB

The the second and subsequent clicks solves my issue, it returns the correct results and will not further return duplicate "ID".

Response:

[

{

"ID": 1,

"Item": "Earth Science Deep Conditioning Masque For Hair - 2 Fl Oz",

"Qty": 30,

"Container": "Bottle",

"Size": "750ml",

"Reiciendis eos nostrum ut sequi.": 1680,

"Sed quidem aspernatur quisquam ut.": 19920,

"Aut dolorem repellendus iste nisi...": 79170,

"Voluptates laudantium voluptas nam.": null,

"Ipsum voluptatem dolorum commodi.": null

},

{

"ID": 9,

"Item": "Laci Le Beau Maximum Strength Super Dieter's Tea Cinnamon Spice - 12 Tea Bags",

"Qty": 10,

"Container": "Bottle",

"Size": "750ml",

"Reiciendis eos nostrum ut sequi.": null,

"Sed quidem aspernatur quisquam ut.": 30,

"Aut dolorem repellendus iste nisi...": null,

"Voluptates laudantium voluptas nam.": 1510,

"Ipsum voluptatem dolorum commodi.": 17910

}

]

Request ID:

"de40d4fa-06bb-433f-81d0-d667b2e2bca6"

Function logs:

START RequestId: de40d4fa-06bb-433f-81d0-d667b2e2bca6 Version: $LATEST

END RequestId: de40d4fa-06bb-433f-81d0-d667b2e2bca6

REPORT RequestId: de40d4fa-06bb-433f-81d0-d667b2e2bca6 Duration: 70.41 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 75 MB

I wondering why it comes on the second click.

回答1:

Or without an if statement and a race conditions:

exports.handler = (event, context, callback) => {

context.callbackWaitsForEmptyEventLoop = false;

pool.getConnection(function(err, connection) {

connection.query('INSERT INTO table (id, name, email)

VALUES (1, "John Doe", "johndoe@gmail.com")

ON DUPLICATE KEY UPDATE table email = "johndoe@icloud.com";', function (error, results, fields) {

// And done with the connection.

connection.release();

// Handle error after the release.

if (error) callback(error) ;

else callback(null, results);

};

});

});

This assumes id is a primary or unique key in the table.

回答2:

Assuming that this is specifically a NodeJS only answer then the problem is you are = rather than ==. The single equals assigns Id to the value you're trying to compare to, whereas the double equals is the notation for is equal to. There's also === which presents as is identical to (== does not care for type so 5=='5' would be true, whereas fail for identical to).

Below is the fixed code.

var mysql = require('mysql');

var config = require('./config.json');

var pool = mysql.createPool({

host : config.dbhost,

user : config.dbuser,

password : config.dbpassword,

database : config.dbname

});

exports.handler = (event, context, callback) => {

context.callbackWaitsForEmptyEventLoop = false;

pool.getConnection(function(err, connection) {

// IF STATEMENT

connection.query('SELECT id from table where id = ?', [event['id']], function (error, results, fields) {

if (results.length > 0) {

// If TRUE

connection.query('UPDATE table SET email = "'johndoe@icloud.com'" WHERE id = 1;', function (error, results, fields) {

// And done with the connection.

connection.release();

// Handle error after the release.

if (error) callback(error) ;

else callback(null, results);,

// If FALSE

connection.query('INSERT INTO table (id, name, email) VALUES (1, "'John Doe'", "'johndoe@gmail.com'");', event['i'], function (error, results, fields) {

// And done with the connection.

connection.release();

// Handle error after the release.

if (error) callback(error) ;

else callback(null, results);

};

});

});

});

来源:https://stackoverflow.com/questions/63083370/nodejs-if-statement-in-aws-lambda-using-mysql-database

aws lam nodejs mysql_NodeJs IF Statement in AWS Lambda using MySQL database相关推荐

  1. aws lambda_如何通过在本地模拟AWS Lambda来加速无服务器开发

    aws lambda by John McKim 约翰·麦金(John McKim) 如何通过在本地模拟AWS Lambda来加速无服务器开发 (How you can speed up server ...

  2. aws 删除ec2实例_如何在AWS中启动EC2实例

    aws 删除ec2实例 你好朋友, 在本教程中,我们将看到如何立即在AWS中旋转EC2实例. 您应该有权访问AWS控制台.如果您还没有AWS账户,则可以单击此处并在AWS上创建免费套餐. 如何在AWS ...

  3. aws eks_在生产中配置和使用AWS EKS

    aws eks 到现在,我们已经完成了向Amazon EKS ( 工作地点)的迁移,并且集群已经投入生产. 过去,我已经写了一些要点的简短摘要,您可以在这里找到. 当系统正在处理实际流量时,我有了一些 ...

  4. aws fargate_借助Fargate和EKS,AWS甚至可以实现Cloud-ier和Kuberneties-ier

    aws fargate 在本周的re:Invent大会上,AWS宣布了很多很棒的事情. 您应该检查一下他们的页面 ,以了解他们正在做的所有新工作的概况–内容很多,而且很多看起来立即有用. 如果您想了解 ...

  5. aws rds监控慢sql_如何将AWS RDS SQL Server与AWS Glue连接

    aws rds监控慢sql This article gives you an overview of configuring AWS RDS SQL Server with AWS Glue ser ...

  6. aws saa 认证价值大吗?aws认证指的是什么?

    现在有越来越多的朋友开始准备aws saa 认证考试了,但是有一些朋友对这个认证考试还不是很了解,那么aws saa 认证价值大吗?aws saa 认证指的是什么呢?下面就给朋友们详细的来分析一下. ...

  7. AWS认证攻略 – E哥的AWS Solution Architecture Associate 认证攻略

    AWS认证介绍 AWS Certified Solutions Architect 系列认证是亚马逊从2013年开始推出的对云计算架构师能力的官方认证.考试不仅考察应试者对AWS相关云服务的熟悉程度, ...

  8. AWS宣布Amazon EKS在AWS中国(宁夏)区域和AWS中国(北京)区域正式商用

    2020年3月13日,北京,今天,亚马逊旗下公司Amazon Web Services, Inc. (AWS) 宣布,Amazon Elastic Kubernetes Service(Amazon ...

  9. aws s3 静态网站_如何使用AWS S3,CloudFront和命令行管理静态网站

    aws s3 静态网站 by Ben Cheng 通过本诚 如何使用AWS S3,CloudFront和命令行管理静态网站 (How to manage your static websites wi ...

最新文章

  1. Python3 如何优雅地使用正则表达式(详解五)
  2. MongoDB实战(4)MapReduce
  3. LiveVideoStackCon 2020北京站-售票通道关闭倒计时2天
  4. P4199-万径人踪灭【FFT】
  5. 前端学习(3251):样式的模块化
  6. 飞鸽_飞鸽传书_飞鸽传书2010_飞鸽传书2010下载
  7. EDA技术实用教程 | 复习九 | 数据选择器的行为级描述
  8. 手把手教你学项目管理软件project
  9. 安装程序提示本地计算机已存在源,MSI文件安装错误码不再烦
  10. WIN10 激活系统
  11. hadoop学习路线
  12. 腾讯北大合作的稀疏大模型训练加速方案HET入选国际顶会VLDB
  13. 一文了解无聊猿(BAYC)的 NFT 帝国是如何形成的
  14. 如何使用 IntelliJ IDEA(2020.2)构建一个JavaWeb项目
  15. JixiPix Hand Tint Pro for Mac(图片处理软件)
  16. Java8的流特性-常用操作(例)
  17. Maya: Rendering with Arnold 5 Maya教程之Arnold5渲染 Lynda课程中文字幕
  18. SOP运营的六大好处
  19. 乐视三合一体感摄像头--windows下的开发2
  20. Imail 邮件服务器的SMTP配置与 .NET 下使用 System.Net.SmtpClient 发送邮件

热门文章

  1. System Information Viewer系统信息、硬件规格检测器
  2. Spring Cloud Bus + RabbitMq 自动刷新
  3. 和菜鸟一起学linux之V4L2摄像头应用流程【转】
  4. Linux教程:10条秘诀确保Linux桌面安全性
  5. 软件性能测试——瓶颈分析方法,性能测试——瓶颈分析方法
  6. redis.conf
  7. LED,LCD,OLED,miniLED,MicroLED显示详解
  8. 你可能没听过的 Java 8 中的 10 个特性
  9. ubuntu10.04以及10.10安装配置tftp服务
  10. 【前端 · 面试 】HTTP 总结(十一)—— HTTPS 概述