我有正在插入我的数据库的数据。它工作得很好但我知道有更好的方法来做到这一点..我应该如何将所有这些查询压缩成单一的?
数据
data = [{Location:'A',shift:'1'},{Location:'B',Shift:'2'},{Location:'C',Shift:'3'}]
的node.js
//Receive from Angular Server
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
var sql = require('mssql');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.all("/*", function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
next();
});
app.listen(1433, function () { console.log('Example app listening on port 1433!') })
var config = {
server: "T21DOPD1",
database: "ARGYLE_COMPARE",
user: "dop_adm",
password: "dopadm",
port: 1433
};
app.post('/ping', function (req, res) {
res.send(res.body);
var jsondata = JSON.stringify(req.body);
var data = JSON.parse(jsondata);
// data = [{Location: 'A', shift: '1'}, {Location: 'B', Shift: '2'}, {Location: 'C', Shift: '3'}]
sql.connect(config, function(err) {
if(err)
console.log(err);
else
console.log("Connection successful");
var request = new sql.Request();
request
.query('INSERT INTO testDB set ?', data, (err,results)=>{
console.log(err, results);
})
})
});
错误
错误:返回回调(错误,记录集,_this.rowsAffected); TypeError:回调不是函数
答案 0 :(得分:0)
只需通过官方mysqljs doc https://github.com/mysqljs/mysql并找出这一行
嵌套数组转换为分组列表(对于批量插入),例如 [[' a',' b'],[' c'' d']]变成了(' a&# 39;,' b'),(' c',' d')
query('INSERT INTO testDB set ?', [[ 'A', '1'], [ 'B', '2'],[ 'C', '3']], (err,results)=>{
console.log(err, results);
})