问题描述
我正在尝试针对以下任一查询在NodeJS中传递IF语句(IF表中存在数据,[true]更新数据,[false]插入数据):
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);
};
});
});
};
我是NodeJS的新手,我仍在尝试使用上面的代码找出IF STATEMENT.我会及时通知你的.
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针对他的答案的详细信息
基本上,我有2个API.
Basically, I have 2 APIs.
第一个API最初运行,如果存在,则删除canvass_prices.这个我没有任何问题.这是第一个API的结果:
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
这是我遇到的第二个API(请参阅此处的第一个更新).该API将更新由第一个API创建的canvass_prices.
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
第二次及其后的点击解决了我的问题,它返回了正确的结果,并且不会进一步返回重复的"ID".
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.
推荐答案
或者没有if语句和竞争条件:
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);
};
});
});
这假定id
是表中的主键或唯一键.
This assumes id
is a primary or unique key in the table.
这篇关于使用MySQL数据库的AWS Lambda中的NodeJs IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!