1

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.

10
  • Possible duplicate of Capturing javascript console.log? Commented Feb 6, 2019 at 10:08
  • 1
    "The only way I have found how to do this is with this: const errorText = console.log(data);" remove the console.log? const errorText=data; Commented Feb 6, 2019 at 10:10
  • 1
    Possible duplicate of JSON.stringify missing properties I think this should help you ;) This looks exactly like your object! Commented Feb 6, 2019 at 10:13
  • 1
    @EdwardTanguay you might wish to rephrase the question to reflect what the real problem was (i.e. getting the data out of the error object) as opposed to your intended solution (intercepting the console). Notwithstanding you did ask how you could "get the error message in a string in some other way" the rest of the question as written is a classic "XY problem". Commented Feb 6, 2019 at 11:07
  • 1
    Thanks, @Alnitak, I rephrased the title so it better reflects the content and answer. Commented Feb 6, 2019 at 12:07

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your info " a non-enumerable property of the Error object " I had stayed up too late to find out what weird thing happened with the message of Error Object Now I have more information to dig in more, thanks a lot
-2

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

3 Comments

Right, but I get this object from the SQLITE database so I have to deal with it as it is. How can I access this wrongly formatted portion of the object?
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.
This has nothing whatsoever to do with the format of the data in the database. When the OP says he "gets this object from the SQLITE database" he means that it's an object generated by the database engine, not one stored in the database.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.