如何在异步 Node.JS 中使用 MySQL
How to use MySQL in async Node.JS
我来自 PHP,不明白如何用异步 NodeJS 编写一系列 MySQL 查询。例如,从 PHP
编码中拿这个例子:
$mysqli->query("INSERT INTO articles (title) VALUES ('title')");
$article_id = $mysqli->insert_id;
foreach ($tags as $tag) {
$result = mysqli->query("SELECT tag_id from tags WHERE tag='$tag'");
if($result->num_rows==1){
$row=$result->fetch_assoc();
$tag_id=$row['tag_id'];
} else {
$mysqli->query("INSERT INTO tags (tag) VALUES ('$tag')");
$tag_id=$mysqli->insert_id;
}
$mysqli->query("INSERT INTO tag_map (article_id, tag_id) VALUES ($article_id, $tag_id)");
}
我们如何在 Node.JS 中编写此代码?
我的意思是,if 语句并为下一个查询获取 insert_id
。
你可以通过这个获取最新记录的insert id
let query = "insert into `articles` (title) VALUES ('" + title + "')";
db.query(query,(err,result) => {
if(err){
return res.status(500).send(err);
}
console.log(result.insertId)
})
.then((articleid)=>{
tags.forEach((tag)=>{
let tagQuery = "SELECT tag_id from tags WHERE tag='"+tag+"';
// get tag_id from tags
db.query(query,(err,result) => {
// if result length is greater tha 0
if(result.length > 0){
tag_id = result.tag_id // set tag id
}
else{
// insert tag into db
let insertTagQuery = "INSERT INTO tags (tag) VALUES ('"+tag+"')';
db.query(insertTagQuery,(err,result) => {
if(err){
return res.status(500).send(err);
}
let mapTagQuery = 'INSERT INTO tag_map (article_id, tag_id) VALUES ('"+article_id+"', '"+result.tag_id+"')';
db.query(mapTagQuery,(err,result) => {
if(err){
return res.status(500).send(err);
}
console.log(result)
});
}
console.log(result.insertId)
})
})
}) ;
我来自 PHP,不明白如何用异步 NodeJS 编写一系列 MySQL 查询。例如,从 PHP
编码中拿这个例子:
$mysqli->query("INSERT INTO articles (title) VALUES ('title')");
$article_id = $mysqli->insert_id;
foreach ($tags as $tag) {
$result = mysqli->query("SELECT tag_id from tags WHERE tag='$tag'");
if($result->num_rows==1){
$row=$result->fetch_assoc();
$tag_id=$row['tag_id'];
} else {
$mysqli->query("INSERT INTO tags (tag) VALUES ('$tag')");
$tag_id=$mysqli->insert_id;
}
$mysqli->query("INSERT INTO tag_map (article_id, tag_id) VALUES ($article_id, $tag_id)");
}
我们如何在 Node.JS 中编写此代码?
我的意思是,if 语句并为下一个查询获取 insert_id
。
你可以通过这个获取最新记录的insert id
let query = "insert into `articles` (title) VALUES ('" + title + "')";
db.query(query,(err,result) => {
if(err){
return res.status(500).send(err);
}
console.log(result.insertId)
})
.then((articleid)=>{
tags.forEach((tag)=>{
let tagQuery = "SELECT tag_id from tags WHERE tag='"+tag+"';
// get tag_id from tags
db.query(query,(err,result) => {
// if result length is greater tha 0
if(result.length > 0){
tag_id = result.tag_id // set tag id
}
else{
// insert tag into db
let insertTagQuery = "INSERT INTO tags (tag) VALUES ('"+tag+"')';
db.query(insertTagQuery,(err,result) => {
if(err){
return res.status(500).send(err);
}
let mapTagQuery = 'INSERT INTO tag_map (article_id, tag_id) VALUES ('"+article_id+"', '"+result.tag_id+"')';
db.query(mapTagQuery,(err,result) => {
if(err){
return res.status(500).send(err);
}
console.log(result)
});
}
console.log(result.insertId)
})
})
}) ;