[英]How to get property out of JavaScript object which is only appearing in console.log() but not in JSON.stringify()?
I'm getting an error object back from my SQLite database. 我从SQLite数据库中得到了一个错误对象。 When I display it with
console.log(err)
, I get: 当我用
console.log(err)
显示它时,我得到:
{ records: { Error: SQLITE_ERROR: no such table: showcaseUsers errno: 1, code: 'SQLITE_ERROR' } }
{记录:{错误:SQLITE_ERROR:否这样的表:showantUsers errno:1,代码:'SQLITE_ERROR'}}
Yet when I display it with JSON.stringify(err)
, I only get: 但是,当我使用
JSON.stringify(err)
显示它时,我只会得到:
{"records":{"errno":1,"code":"SQLITE_ERROR"}}
{“ records”:{“ errno”:1,“ code”:“ SQLITE_ERROR”}}
I want to get the error message no such table: showcaseUsers
in a string. 我想得到错误消息,
no such table: showcaseUsers
字符串中的no such table: showcaseUsers
。
The only way I have found how to do this is with this: 我发现如何执行此操作的唯一方法是:
const errorText = console.log(data);
But this also outputs the data to the console, which is undesirable. 但这也将数据输出到控制台,这是不希望的。
How can I either (1) stop console.log from outputting its content to the console, or else (2) get the error message in a string some other way? 我如何(1)阻止console.log将其内容输出到控制台,或者(2)以其他方式获得字符串错误消息?
NOTE: I am in Node at this point and not in a browser, so it doesn't seem that the answer at Capturing javascript console.log? 注意:我目前在Node中,而不是在浏览器中,因此,似乎没有在捕获javascript console.log上找到答案? is helpful.
是有帮助的。
There's no need to intercept the console
at all - you will find the entire error message in err.message
, albeit still prefixed with " SQLITE_ERROR:
" 根本不需要拦截
console
-您将在err.message
找到整个错误消息,尽管仍然以“ SQLITE_ERROR:
”为前缀
It's a non-enumerable property of the Error
object hence why it doesn't appear in the JSON output: 这是
Error
对象的不可枚举的属性,因此为什么它不出现在JSON输出中:
let sqlite3 = require('sqlite3');
let db = new sqlite3.Database(':memory:');
db.run("UPDATE foo SET bar = 1", (err, res) => {
console.log(Object.getOwnPropertyDescriptors(err));
console.log(err.message);
});
with output: 输出:
{ stack:
{ value: 'Error: SQLITE_ERROR: no such table: foo',
writable: true,
enumerable: false,
configurable: true },
message:
{ value: 'SQLITE_ERROR: no such table: foo',
writable: true,
enumerable: false, <<---------
configurable: true },
errno:
{ value: 1, writable: true, enumerable: true, configurable: true },
code:
{ value: 'SQLITE_ERROR',
writable: true,
enumerable: true,
configurable: true } }
SQLITE_ERROR: no such table: foo
Issue is wrongly formatted JSON, which must be key:value
pairs. 问题是格式错误的JSON,它必须是
key:value
对。 If value
in key:value
is a string then it should be enclosed in single or double quotes. 如果
value
在key:value
是一个字符串,那么就应该用单引号或双引号。
{ records: { Error: SQLITE_ERROR: no such table: showcaseUsers errno: 1, code: 'SQLITE_ERROR' } }
{记录:{错误:SQLITE_ERROR:否这样的表:showantUsers errno:1,代码:'SQLITE_ERROR'}}
In the absence of proper value
( value
in your case has datatype String
), JSON.stringify(err)
tries to coerce it to JSON based on colon :
as separator between key
and value
. 在没有适当
value
的情况下(您的value
数据类型为String
), JSON.stringify(err)
尝试基于冒号将其强制转换为JSON :
作为key
和value
之间的分隔符。 Available datatypes in JavaScript JavaScript中的可用数据类型
Here is a correction, I have just enclosed it in double quotes, and formatted it (formatting is optional, helpful for legibility). 这是一个更正,我将其括在双引号中并对其进行了格式化(格式化是可选的,有助于提高可读性)。
{
records: {
Error: "SQLITE_ERROR: no such table: showcaseUsers errno: 1, code: 'SQLITE_ERROR'"
}
}
Actually it should be formatted (see placement of single quotes) like so 实际上,它应该像这样格式化(请参阅单引号的放置)
{
records: {
Error: 'SQLITE_ERROR: no such table: showcaseUsers',
errno: 1,
code: 'SQLITE_ERROR'
}
}
Update: It's imperative to store properly formatted data in database in the first place. 更新:首先必须将格式正确的数据存储在数据库中。 If you missed it then you are left with manually parsing data you are getting from SQLite.
如果您错过了它,那么您将不得不手动解析从SQLite获取的数据。
Helpful resources 有用的资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.