简体   繁体   English

如何从仅出现在console.log()中而不出现在JSON.stringify()中的JavaScript对象中获取属性?

[英]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. 如果valuekey: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 :作为keyvalue之间的分隔符。 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.

相关问题 console.log与JSON.stringify不一致 - console.log inconsistent with JSON.stringify JSON.stringify 返回不同的值,然后是 console.log - JSON.stringify returning different values then a console.log console.log(obj) 和 console.log(JSON.stringify(obj)) 的区别 - Difference between console.log(obj) and console.log(JSON.stringify(obj)) 为什么 var clientPeople = &lt;%- JSON.stringify(serverPeople) %&gt;; console.log(typeof clientPeople); 在 ejs 文件中作为对象而不是字符串返回 - why var clientPeople = <%- JSON.stringify(serverPeople) %>; console.log(typeof clientPeople); returns as object not string inside of ejs file 为什么console.log(窗口)工作但JSON.stringify(窗口)不起作用,我怎么能打败它? - Why console.log(window) works but JSON.stringify(window) doesn't work, and how can I beat that? 我 JSON.stringify 一个对象,但如何在 Javascript 中打印出对象的特定元素 - I JSON.stringify an object but how do I print out specific elements of object in Javascript 如何使用 get/set 获取属性以使用 JSON.stringify() 进行序列化 - How to get a property with get/set to serialize with JSON.stringify() Javascript对象属性Console.log - Javascript Object Property Console.log 如何使用 JavaScript 在没有 JSON.stringify 的情况下将对象转换为字符串? - How to convert object to string without JSON.stringify using JavaScript? JSON.stringify()是JavaScript的对象和函数标准吗? - JSON.stringify() is this object and function standard to javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM