async.each表插入麻烦?(async.each table insertion trouble?)

编程入门 行业动态 更新时间:2024-10-28 12:21:32
async.each表插入麻烦?(async.each table insertion trouble?)

我使用async.each编写了一个代码,用于将数据插入表中。

var categoryList = [{"categoryName": "biriyani","productName":"chicken biriyani"}, {"categoryName":"biriyani","productName":"mutton biriyani"}] async.each(categoryList, function(item,callback) { var categoryName=item.categoryName; var productName=item.productName; var categoryCheckQuery = pgFormat("select * from shop where categoryName LIKE '%"+categoryName+"%'"); model.client.query(categoryCheckQuery,function (err,result) { if(result.rowCount==0){ var insertCategoryQuery = pgFormat("insert into shop(categoryName)values(%L)",categoryName); model.client.query(insertCategoryQuery,function (err,result) { if (!err) { console.log("success"); } }); } else{ //insert product into product table } });

说明:

1)这里首先将包含categoryName-> biriyani的Json数组输入到商店表中

2)当async.each获取包含categoryName-> biriyani的下一个json数组时,categoryCheckQuery检查商店表是否已存在categoryname ='biriyani'。

3)如果存在,它将不会被保存

问题:

这里对于数据result.rowCount ==0并且将具有categoryname = biriyani的数据输入到商店表中。

I wrote a code using async.each for inserting data into tables.

var categoryList = [{"categoryName": "biriyani","productName":"chicken biriyani"}, {"categoryName":"biriyani","productName":"mutton biriyani"}] async.each(categoryList, function(item,callback) { var categoryName=item.categoryName; var productName=item.productName; var categoryCheckQuery = pgFormat("select * from shop where categoryName LIKE '%"+categoryName+"%'"); model.client.query(categoryCheckQuery,function (err,result) { if(result.rowCount==0){ var insertCategoryQuery = pgFormat("insert into shop(categoryName)values(%L)",categoryName); model.client.query(insertCategoryQuery,function (err,result) { if (!err) { console.log("success"); } }); } else{ //insert product into product table } });

Explanation:

1)Here first Json array containing categoryName->biriyani is entered into shop table

2)when async.each fetching next json array containing categoryName->biriyani, categoryCheckQuery checks the shop table whether categoryname = 'biriyani' is already exists.

3)If exists it wont be saved

Problem:

Here for both the data result.rowCount ==0 and both the data which have categoryname = biriyani is entered into shop table.

最满意答案

这段代码中有几个问题。

一个是使用async.each(),应该使用async.eachSeries()而不是async.each(),因为categoryList中下一个项的操作取决于当前项的操作。 async.eachSeries()确保在继续下一个项目之前完成第一个项目。

另一个是async.each()的callback()应该被调用来通知它你完成了它。

这是修改后的代码:

var categoryList = [{"categoryName": "biriyani","productName":"chicken biriyani"}, {"categoryName":"biriyani","productName":"mutton biriyani"}] // Use async.eachSerices() instead of async.each() async.eachSeries(categoryList, function(item,callback) { var categoryName = item.categoryName; var productName = item.productName; var categoryCheckQuery = pgFormat("select * from shop where categoryName LIKE '%" + categoryName + "%'"); model.client.query(categoryCheckQuery, function (err, result) { if (result.rowCount == 0) { var insertCategoryQuery = pgFormat("insert into shop(categoryName)values(%L)", categoryName); model.client.query(insertCategoryQuery, function (err, result) { if (!err) { console.log("success"); } // passing non-null if you want to stop async.eachSeries() in case of error callback(null); // <<<<< need to call async.each()'s callback() here }); } else { //insert product into product table doInsert(params, function(err, result) { callback(null); // <<<<< need to call async.each()'s callback() here }); } }); });

此外,检查返回的错误可能是一种很好的做法..在这种情况下特别是model.client.query()

There are couple problems in this code.

One is the use of async.each(), async.eachSeries() should be used instead of async.each() because the operation of the next item in categoryList depends on current item's operation. async.eachSeries() ensures that first item is done before moving on to next item.

Another is async.each()'s callback() should be called to signal it that you're done with it.

Here is the revised code:

var categoryList = [{"categoryName": "biriyani","productName":"chicken biriyani"}, {"categoryName":"biriyani","productName":"mutton biriyani"}] // Use async.eachSerices() instead of async.each() async.eachSeries(categoryList, function(item,callback) { var categoryName = item.categoryName; var productName = item.productName; var categoryCheckQuery = pgFormat("select * from shop where categoryName LIKE '%" + categoryName + "%'"); model.client.query(categoryCheckQuery, function (err, result) { if (result.rowCount == 0) { var insertCategoryQuery = pgFormat("insert into shop(categoryName)values(%L)", categoryName); model.client.query(insertCategoryQuery, function (err, result) { if (!err) { console.log("success"); } // passing non-null if you want to stop async.eachSeries() in case of error callback(null); // <<<<< need to call async.each()'s callback() here }); } else { //insert product into product table doInsert(params, function(err, result) { callback(null); // <<<<< need to call async.each()'s callback() here }); } }); });

Also, it's probably good practice to check for error returned.. specifically model.client.query() in this case

更多推荐

本文发布于:2023-07-20 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1203652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:麻烦   async   table   trouble   insertion

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!