I'm writing a utility to help me with some debugging tasks. It collects all the JavaScript errors on a page, and sends them to a PHP script via AJAX (where they are written to a log file).
In the JS file, the data I want about each error is used to create an object for each error, which is then added to an overall array of errors.
let errors = []; $(window).on('error', function(event) { let error = { url: window.location.href, message: event['originalEvent']['message'], file: event['originalEvent']['filename'], line: event['originalEvent']['lineno'] }; errors.push(error); });
When the window has finished loading, the array of errors is then sent to PHP via an AJAX function. Here is an example of the output of console.log(errors)
:
[ { "url": "http://mysite.local/", "message": "TypeError: can't access property \"add\", e.$slides is null", "file": "http://mysite.local/wp-content/themes/mytheme/scripts/vendor/slick.min.js?ver=1", "line": 1 }, { "url": "http://mysite.local/", "message": "Script error.", "file": "https://maps.googleapis.com/maps/api/js?key=KEY_HERE", "line": 0 } ]
The issue I am having with this is that JSON.stringify(errors)
is only using the first error. Here's the output of JSON.stringify(errors)
for the above example:
[{"url":"http://mysite.local/","message":"TypeError: can't access property \"add\", e.$slides is null","file":"http://mysite.local/wp-content/themes/mytheme/scripts/vendor/slick.min.js?ver=1","line":1}]
Every post I've found so far implies that JSON.stringify should take the whole array. I've also tried using an object with nested objects instead of an array of objects but the same issue occurs. What am I doing wrong?
https://stackoverflow.com/questions/65911515/only-first-object-is-included-when-using-json-stringify-on-an-array-of-objects January 27, 2021 at 09:06AM
没有评论:
发表评论