Estoy intentando de registrar múltiples datos en una BD SQL server, utilizando Node.js de la siguiente forma:
for(var i = 0;i<cantidad;i++){
var vuelta = i+1;
records.push([respuesta,vuelta+'/'+cantidad,hoy,req.query["id_usuario"],req.query["descripcion"],req.query["proveedor"]]);
console.log(records)
}
var cons = 'insert into registros (id_insumo,cantidad,fecha,id_usuario,observacion,lote_proveedor) values ?';
var request2 = new sql.Request();
console.log(req.query)
request2.query(cons,[records] , function (err, rows) {
if(!err){
res.send(rows['recordsets'][0]);
console.log(rows)
} else{
res.send(err)
}
});
De esta forma es posible completar la lista de forma dinamica y luego entregarla en una unica consulta que inserte todo a la BD. Este metodo me funciona correctamente con MYSQL pero en SQL server me envia error.
Este es el error en concreto:
node:6248) UnhandledPromiseRejectionWarning: RequestError: Incorrect syntax near '?'.
at handleError (c:\AppInsumo\node_modules\mssql\lib\tedious\request.js:366:15)
at Connection.emit (events.js:198:13)
at Parser.tokenStreamParser.on.token (c:\AppInsumo\node_modules\tedious\lib\connection.js:827:12)
at Parser.emit (events.js:198:13)
at Parser.parser.on.token (c:\AppInsumo\node_modules\tedious\lib\token\token-stream-parser.js:37:14)
at Parser.emit (events.js:198:13)
at addChunk (c:\AppInsumo\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:297:12)
at readableAddChunk (c:\AppInsumo\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:279:11)
at Parser.Readable.push (c:\AppInsumo\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:240:10)
at Parser.Transform.push (c:\AppInsumo\node_modules\tedious\node_modules\readable-stream\lib\_stream_transform.js:139:32)
(node:6248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:6248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Saludos.