Mor*_*rph 5 javascript mysql node.js
我目前正在将一个旧的 PHP 项目迁移到 Node.js,并且正在努力使用 mysqljs/mysql 模块。
我必须执行批量插入 - 到目前为止一切顺利,效果很好。但我还需要所有 insertId。不幸的是我只在回调中得到第一个 insertId 。
假设变量“nestedDataArray”有四个要插入的数据数组 - 所以我们应该得到 4 个新行。这是一个例子:
db.query({
sql: 'INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES ?;',
timeout: 5000,
values: [ nestedDataArray ] // This works as intended.
}, function(error, result){
if (error) {
// ... handle error
} else {
var id = result.insertId; // Here I get only the first one. What about the other three inserts?
db.query( /* Yes, there are nestes queries. */ );
}
});
Run Code Online (Sandbox Code Playgroud)
在 for 循环中移动 db.query 不起作用,因为在我的“真实”代码中,我必须嵌套多个查询,这些查询取决于先前执行的查询的查询。
是否可以从批量插入中获取所有 insertId?
先感谢您!
| 归档时间: |
|
| 查看次数: |
2094 次 |
| 最近记录: |