简体   繁体   中英

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. When I display it with console.log(err) , I get:

{ records: { Error: SQLITE_ERROR: no such table: showcaseUsers errno: 1, code: 'SQLITE_ERROR' } }

Yet when I display it with JSON.stringify(err) , I only get:

{"records":{"errno":1,"code":"SQLITE_ERROR"}}

I want to get the error message no such table: showcaseUsers in a string.

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?

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? 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: "

It's a non-enumerable property of the Error object hence why it doesn't appear in the JSON output:

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. If value in key:value is a string then it should be enclosed in single or double quotes.

{ records: { Error: SQLITE_ERROR: no such table: showcaseUsers errno: 1, code: '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 . Available datatypes in 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.

Helpful resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM