如何使用node.js插入批量JSON数据?

时间:2017-02-27 09:55:58

标签: javascript json node.js promise es6-promise

我想在这里实现的是我想将这个JSON插入我的数据库,这是我的数据库正在使用sql server。我有批量json数据,其中包含json树的详细信息。下面是JSON数据,我想插入

[
   {
      No_BPS:'BSWEB12345',
      Kd_Plg:'MMIM026',
      Nm_Plg:'YAOMING',
      Tgl_BPS:'2017-02-27T08:39:38.971Z',
      Entry_Time:'2017-02-27T08:39:38.971Z',
      User_Name:'tes',
      Status:'Y',
      Kd_Teknisi:'tes',
      No_Ref:'RIRIMIMI609-001',
      detailsData:[
         {
            No_BPS:'BSWEB12345',
            Kd_Brg:'RH-C779-SB',
            Qty:2,
            Alasan:'undefined',
            Keterangan:'undefined'
         }
      ]
   },
   {
      No_BPS:'BSWEB99999',
      Kd_Plg:'PTPS053',
      Nm_Plg:'WARLORD',
      Tgl_BPS:'2017-02-27T08:40:04.082Z',
      Entry_Time:'2017-02-27T08:40:04.082Z',
      User_Name:'tes',
      Status:'Y',
      Kd_Teknisi:'tes',
      No_Ref:'PTKP1210-001',
      detailsData:[
         {
            No_BPS:'BSWEB99999',
            Kd_Brg:'PS-230BIT SNI',
            Qty:1,
            Alasan:'undefined',
            Keterangan:'undefined'
         },
         {
            No_BPS:'BSWEB99999',
            Kd_Brg:'PS-130BIT SNI',
            Qty:1,
            Alasan:'undefined',
            Keterangan:'undefined'
         }
      ]
   }
]

这是我的nodejs代码:

exports.insert_BPS = function(req, resp) {
    console.log(req.body);
    req.body.map(headerdata=>
        {
            var sql = `INSERT INTO TblBPSHeader (
                        No_BPS, Kd_Plg, Tgl_BPS, Entry_Time, User_Name, Status, Kd_Teknisi) values
                        ('` + headerdata.No_BPS + `', 
                        '` + headerdata.Kd_Plg + `',
                        '` + dateFormat(headerdata.Tgl_BPS, "yyyy-mm-dd' 'HH:MM:ss") + `',
                        '` + dateFormat(headerdata.Entry_Time, "yyyy-mm-dd' 'HH:MM:ss") + `',
                        '` + headerdata.User_Name + `',
                        '` + headerdata.Status + `', 
                        '` + headerdata.Kd_Teknisi + `')`

            db.executeSql(sql, function(data, err) {
                if (err) {
                    httpMsgs.show500(req, resp, err);
                } else {
                    // httpMsgs.send200(req, resp);
                    console.log('header inserted');
                };
            });
            Promise.all(headerdata.detailsData.map
                (detaildata => 

                    {
                        console.log(detaildata);
                        var sqldetail = `INSERT INTO TblBPSDetail (
                                    No_BPS, kd_Brg, Qty, Alasan, Keterangan) values
                                    ('` + detaildata.No_BPS + `', 
                                    '` + detaildata.Kd_Brg + `',
                                    '` + detaildata.Qty + `',
                                    '` + detaildata.Alasan + `',
                                    '` + detaildata.Keterangan + `')`

                        db.executeSql(sqldetail, function(data, err) {
                            if (err) {
                                httpMsgs.show500(req, resp, err);
                            } else {
                                // httpMsgs.send200(req, resp);
                                console.log('detail inserted');
                            };
                        });
                    }
                )
            )
        }
    )
};

但结果是数据被插入但它是重复的,它总是循环数据时,在第一次插入数据后它成功插入所有数据,但是当我离开它一会儿时,它会再次插入它自己所以数据会重复并导致重复错误。如何正确插入批量数据json?

1 个答案:

答案 0 :(得分:0)

猜测你希望Promises能够使这段代码正常工作,尽管你的代码似乎根本没有使用Promise - 尽管随机Promise.all埋在那里!

首先,要使用Promises,你需要一个“有希望的”exectueSql - 一旦你有了它,其余部分实际上是非常直接的

exports.insert_BPS = function(req, resp) {
    // a promisified executeSql
    let executeSql = (db, sql) => new Promise((resolve, reject) => db.executeSql(sql, (err, data) => {
        if (err) {
            return reject(err);
        }
        resolve(data);
    }));

    return Promise.all(req.body.map(headerdata => {
        var sql = `INSERT INTO TblBPSHeader (
                    No_BPS, Kd_Plg, Tgl_BPS, Entry_Time, User_Name, Status, Kd_Teknisi) values
                    ('` + headerdata.No_BPS + `', 
                    '` + headerdata.Kd_Plg + `',
                    '` + dateFormat(headerdata.Tgl_BPS, "yyyy-mm-dd' 'HH:MM:ss") + `',
                    '` + dateFormat(headerdata.Entry_Time, "yyyy-mm-dd' 'HH:MM:ss") + `',
                    '` + headerdata.User_Name + `',
                    '` + headerdata.Status + `', 
                    '` + headerdata.Kd_Teknisi + `')`
        return executeSql(db, sql)
        .then(() => Promise.all(headerdata.detailsData.map(detaildata => {
            var sqldetail = `INSERT INTO TblBPSDetail (
                                No_BPS, kd_Brg, Qty, Alasan, Keterangan) values
                                ('` + detaildata.No_BPS + `', 
                                '` + detaildata.Kd_Brg + `',
                                '` + detaildata.Qty + `',
                                '` + detaildata.Alasan + `',
                                '` + detaildata.Keterangan + `')`
            return executeSql(db, sqldetail);            
        })));
    })).then(results => {
        // all done here
        httpMsgs.send200(req, resp);
    }).catch(reason => {
        httpMsgs.show500(req, resp, reason);
    });
};