Node.js/MySQL:在node.js的错误日志中打印实际查询

编程入门 行业动态 更新时间:2024-10-28 18:36:47
本文介绍了Node.js/MySQL:在node.js的错误日志中打印实际查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些Node.js代码,尝试通过以下方式更新数据库:

I have some Node.js code that tries to update a database in something like the following:

connection.query(command, function(err,rows) { if (err){ console.log(command); console.log("ERROR"); console.log(err); return; } console.log("good"); });

针对不同的"command"值重复运行以上操作,从而对数据库生成不同的查询.问题在于,当出现错误时,错误的查询将打印在console.log(command)中.这是因为将查询添加到队列的时间与实际执行查询的时间不同,因此每次这些时间的"command"值都不相同.有办法解决吗?

The above is run repeatedly for different values of "command", thus generating different queries to the database. The problem is that when there is an error, the wrong query gets printed in the console.log(command). This is because the time the query is added to the queue, and the time the query is actually executed are not the same, so the value of "command" at each of these times isn't the same. Is there a way around this?

注意:console.log(err)打印错误本身以及查询的一部分,但仅打印发生错误的行.我想打印整个查询.

Note: console.log(err) prints the error itself, and also part of the query, but it only prints the line where the error occurred. I want to print the whole query.

推荐答案

根据 docs ,您可以使用query.sql来获取实际执行的查询.

As per docs, You can use query.sql to get the actual executed query.

var post = {id: 1, title: 'Hello MySQL'}; var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) { // Neat! }); console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'

在这种情况下,它将是

connection.query(command, function (err, rows) { if (err) { console.log('this.sql', this.sql); //command/query console.log(command); console.log("ERROR"); console.log(err); return; } console.log("good"); });

更多推荐

Node.js/MySQL:在node.js的错误日志中打印实际查询

本文发布于:2023-07-26 14:28:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1216701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   日志   js   Node   node

发布评论

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

>www.elefans.com

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