JSON.stringify missing properties - javascript

I'm writing a custom console.error function so that every time an error occurs I receive an e-mail. To send the error in e-mail body I use JSON.stringify(). The problem is that it is missing some properties. See the two images below:
Email:
In console:
And here is how I use JSON.stringfy:
JSON.stringify(arguments, null, 4);
I've been googling and found people with the same issue, but so far no answer.
Could you help me, please?

Edit : See this.
Since your Error object is inside another object, you might have to use 2 stringify calls :
JSON.stringify({
0: ...,
1: JSON.stringify({errorObject, ["message", "arguments", "type", "name"]}),
});
Or something like that.
If I'm getting this correctly, the informations you are lacking are in the Error object. My guess would be that JSON.stringify calls .toString() for each object inside it. Though, for an Error object, maybe the toString() function doesn't return ALL the informations you want, versus what you see in the console.
Maybe you'll have to call the Error object's .description() function yourself.

Related

Why we use console.table method?

I know one of the most basic debugging tools in JavaScript is console.log(). The console comes with several other useful methods that can add to a developer’s debugging toolkit.
but
I'm pretty confused about console.table() method. Is anyone explain?
The Console Object:
The console object gives you access to the browser’s console. It lets you output strings, arrays, and objects that help debug your code. The console is part of the window object, and is supplied by the Browser Object Model (BOM).
There are four different ways of outputting a message to the console:
log
info
warn
error
while
there is another method that is:
console.table()
let me explain
The table method displays an array or object as a table.
console.table(['Javascript', 'PHP', 'Perl', 'C++']);
example:
const superhero = {
firstname: 'Peter',
lastname: 'Parker',
}
console.table(superhero);

Object property displayed in the console, but returns undefined when I read it

article is an object variable.
console.log("reesult id " + JSON.stringify(article));
When I do this, it outputs me: [{"Id":43}]
and when I do:
console.log(article[0]);
It outputs me {"Id":43}
but now... HOW to get just 43?
Because when I type:
console.log(article[0].Id);
, It returns me undefined instead of 43. Pf.
So, HOW to get 43?
It is very difficult because I made researches and it does not work as well.
I am unable to comment to so posting as an answer.
Your solution looks fine.
Could you print the output for
JSON.stringify(article[0].id)
Sequelize has it's own way how to return objects in json so you should try using their way to handle with objects since you are using that library.
This is from Sequelize documentation.
For any response object that resulted from a query, you can extract
only the data you want by appending .get({plain:true}) the the
response.
Here you have a link to that documentation so you can read more for that since you are not providing any code to us.

How do I access this Javascript mysql error object (Nodejs)?

More details below, but the question is is the below a valid javascript object and how do I access that first array type thing
{ [some-text-here-without-quotes] key1: 'text', key2: 1, key3: '23000', key4: 0 }
Building an app with Nodejs, Express and MySQL.
I have decided to use the 'mysql' library found in NPM at this link: https://www.npmjs.com/package/mysql.
While testing I purposely created a duplicate entry condition that violated a unique constraint because I wanted to see what the returned error object looks like.
When I console.log(error) from the query the following is what prints:
{ [Error: ER_DUP_ENTRY: Duplicate entry 'abcdefg' for key 'token_UNIQUE'] code: 'ER_DUP_ENTRY', errno: 1062, sqlState: '23000', index: 0 }
I must be missing something obvious. But I cannot seem to access the data in that first array looking thing. The part of the object that starts with [Error: and ends with 'token_UNIQUE']
How do I access that part of the object?
(also, first I thought it was an improperly formatted object string or something buggy from the mysql library but underscore.js says it is an object
_.isObject(error); //true
got the exact same problem, and i still dont understand what that particular syntax means..
however, i managed to access this part of the object by force casting it into a string, by merely concatenating it to a string contant :
if (error) {
error.isError=1
errmsg=" "+error <---- cast to string here
error.msg=errmsg; <---- then back into the object as a tring
response.end(JSON.stringify(error));
db.end();
console.log("<-"+error)
return;
}
You can do
Object.keys(err)
This will give you the list of keys that are contained in the err object. From that, you can pick the keys of your intereste. (or) you can use,
console.dir(err)
to print the object in a formatted way.
maybe the answer is a little bit late, I encountered this problem yesterday. but the solution is simple, just use toString()
error.toString()
error.message maybe a formal way
both methods above returning the same output

JSON.parse not evaluating JSON strings properly

I am using JSON.parse to parse this JSON string
[{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}]
However I am simply getting this result as the output:
[object Object]
Which shouldn't be the result. I am using this within the Cappuccino framework. Does anyone know what I am doing wrong here?
[object Object] is what objects display when you call toString on them. It looks like you're taking your result and trying to call obj.toString()
Also, your JSON is an array with one element in it, so to verify that your result is correct, you can access the name property on the [0] index:
obj[0].name // should be "joe".
var text = '[{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}]';
var obj = JSON.parse(text);
alert(obj[0].name); //alerts joe
DEMO
Or get rid of the array, since it's not really doing much
var text = '{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}';
var obj = JSON.parse(text);
alert(obj.name); //still joe
DEMO
This is an array because it's in square brackets - [] - remove these and it should work...
Even though this is 'syntactically' correct the parser sees this as an array (which is a type of object) but won't do the work on it the way you'd expect.
Also for future reference:
Try to lint it, and see if your syntax is messed up: http://jsonlint.com/
This is an old subject, but nonetheless, I spent hours trying to figure out what was going on. So, hopefully this will help someone else in the future.
My issue was setting up a simple ajax call, and doing something with the resultset from said ajax call. However, no matter what I did, I couldn't get the json resultset to an object.
I ended up stepping through everything in the debugger window and noticed old code that was no longer active was showing up on the sidebar (dom detail). So, my data had been cached. I cleared the cache, and boom! Everything worked.

How to print an object on the developer tools console in Javascript

Please help me in printing the complete object using console.log method. If I am simply putting the name of the object it simply prints [object] [object]. But I want to print the object with complete hierarchy.
For an example, I am getting object in the following method,
getObject : function(responseObj) {
console.log('Object hierarchy is'+responseObj)
}
This simply returns [object] but I want to see the complete hierarchy in the developer tools. Please see, I am using sencha.
Use console.dir, and don't concatenate...
console.dir( responseObj );
Or if you want a label, use a comma to pass multiple arguments.
console.log('Object hierarchy is:', responseObj)
The problem here is that you are concatenating an object onto a string, so it's not doing what you are expecting. Instead of putting it all into one console.log call, do two, the first with the text you want, and the second with just the name of the object in it.
getObject : function(responseObj) {
console.log('Object hierarchy is:');
console.log(responseObj);
}
edit:
If you are logging into a text/non-interactive console, that doesnt let you explore the object, you'll need to implement a custom inspection function, something like this gist will get you started. What this is doing is echoing out the property name, and the value it finds step by step, but pay attention to what it says there about scoping.
edit edit:
didn't know about console.log taking multiple parameters, never needed it :o handy though!

Categories

Resources