Working with a third-party database using a REST API, I encountered an error response (as expected). Here is the code snippet:
transaction.commit(function(err) {
if (err){
var par = JSON.parse(err); \\ leading to error: SyntaxError: Unexpected token E in JSON at position 0
console.log(JSON.stringify(err));
console.log(err);
console.log('' + err);
//The outputs of the logs are as follows:
//First console.log output: {"code":409,"metadata":{"_internal_repr":{}}}
//Second console.log output: { Error: entity already exists: app: "s~myapp"<br/>path <<br/> Element {<br/> type: "v"<br/> name: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f3919c91">[email protected/></a>"<br/> }</br/>><br/>
//Third console.log output: Error: entity already exists: app: "s~myapp"<br/>path <<<br/> Element {<br/><br/> type: "v"<br/> name: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8defe2efcd<a>eae<b>4ece4e1<i>a3eee2e0</i>[email pro</b><a>tected]</i></a>"<br/> }<<br/>><<<<br/>
}
{);
To retrieve the error and type fields, I attempted parsing the JSON object. However, there were parsing errors prohibiting me from accessing par.error or par.type variables. https://i.sstatic.net/hHHlc.png