Extracting the call from REST API in JSON format

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

Answer №1

It seems like you're dealing with an Error object that contains a message property where the error message is stored.

To access this error message, simply use:

err.message

in your code.

For more information, you can refer to these resources:

Answer №2

After reviewing your findings, it appears that the `err` parameter you are receiving is already an object and not a JSON string, eliminating the need to parse it.

You should have no trouble accessing `err.code` directly.

Although you mentioned the need for the error type, it seems that information is not included in the object (hence the `undefined` result when attempted).

However, using `err.Error` should allow you to retrieve the error string.

If you are uncertain about the object's data, consider the following steps:

  1. Execute `console.dir(err)` - this will provide insight into the contents of the `err` object.
  2. Debug up to that point and explore the `err` object further.
  3. (Recommended) Refer to the platform's or libraries' API documentation for details on the error information it provides.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamically linking tabbable tabs is a useful technique that allows for

I have been working on an app that organizes websites into groups displayed as tabs in a tabbable navigator using Twitter Bootstrap. The idea is that each time a new group is added, a new tab should appear. Currently, this is how it looks: The functionali ...

Should you stick with pre-defined styles or switch to dynamic inline style changes?

I am currently developing a custom element that displays playing cards using SVG images as the background. I want to make sure that the background image changes whenever the attributes related to the card's suit or rank are updated. From what I under ...

The changes made to the state array in react.js are not reflected in the DOM

In my React game implementation, I have the board set up as a two-dimensional array in the initial state of the parent component. The tiles on the board are rendered by looping through this array. Each tile receives a function as a prop that allows it to m ...

Having issues with regEX functionality in an Angular form

I need to validate a phone number using regEX. My criteria is as follows: 10 digits alpha/numeric, where an Alpha CHAR is in the 4th position (excluding hyphens). For example: 586R410056  NNN ANN NNNN  (NNN) ANN NNNN  NNN-ANN-NNNN  (NNN) AN ...

Utilizing various filters and sorting options on API response within Angular 8

Upon receiving the following API response: [ { "imgPaths":[ "gallery/products/55ccb60cddb4d9bded02accb26827ce4" ], "_id":"5f3e961d65c6d591ba04f3d3", "productName":" ...

Using ajax to send an array to PHP

I have an array named "heart" that is being sent via ajax... var heart = [31,32,33,34,35,36,37,38,39,42,43]; // Sending this data via ajax to php file/ $.ajax({ type: "POST", data:{ 'system': heart }, url: "login-function.php", success: f ...

How to properly declare an explicit injector when using the resolve parameter in $routeProvider?

$routeProvider resolve feature in AngularJS allows for injecting additional dependencies to the controller function. How can we combine this with explicit dependency injection declaration? Example: angular.module('myModule', []) .config(func ...

HackerRank Challenge: Strategies for Efficiently Solving Minimum Swaps 2

In this challenge, the goal is to determine the minimum number of swaps needed to arrange an array of disordered consecutive digits in ascending order. My code successfully handles most of the tests, but I'm encountering timeout errors with four speci ...

Experiencing trouble with implementing multiple textboxes based on option selection using javascript

My JavaScript code is supposed to display multiple textboxes based on the user's selection, but for some reason, I can only select one textbox at a time. I'm not sure what's wrong with my code. Here's the snippet of the code: <ht ...

JavaScript keydown event for rotating images

I am experiencing an issue with JavaScript animation. I have incorporated code from this particular link into my keydown function. However, the code is not functioning as expected. While the code from the provided link works fine on its own, within the key ...

What are the steps to use the vue-text-highlight feature?

I am attempting to implement an example from the basic usage section of https://github.com/AlbertLucianto/vue-text-highlight. However, upon opening index.html in Firefox, I am only greeted with a blank page. You can find the code I am using at https://git ...

The custom attribute in jQuery does not seem to be functioning properly when used with the

I am currently working with a select type that includes custom attributes in the option tags. While I am able to retrieve the value, I am experiencing difficulty accessing the value of the custom attribute. Check out this Jsfiddle for reference: JSFIDDLE ...

Exploring the possibilities of developing WebComponents within Angular using TypeScript

My Angular application was originally created using the default method with ng new project-name". However, for performance reasons, I had to incorporate single standard WebComponents. The JavaScript code associated with these components is stored in a ...

What is the best way to determine which user is currently logged in to a Linux system?

Understanding the user on whose behalf the program is operating is crucial. If it's running as root, proceed with code execution. If it's a regular user, execute the bash command through a child process. ...

One of the two identical pages is experiencing issues with the jQuery leanmodal while the other is

Despite having the same scripts and libraries loaded in two pages with identical templates, there is a slight difference in main content. Strangely, leanmodal only seems to work on the index page and not on any other page. <script type="text/javascrip ...

State variable in Redux is not defined

While there have been numerous similar inquiries on this platform, I haven't been able to find a solution to my particular issue. Below is the React component in question: class MyTransitPage extends Component { componentDidMount() { this.pro ...

Using command line arguments to pass parameters to package.json

"scripts": { "start": "gulp", ... }, I have a specific npm package that I'm using which requires passing parameters to the start command. Can anyone help me with how to pass these parameters in the command line? For example, is it possible ...

What could be causing the error during the build process when using eslint version 5.6.0?

After creating a react app using the react-scripts package, I encountered an issue where it required eslint ^5.6.0 as a dependency. However, there was another version (8.30.0) detected higher up in the tree, which is the latest one I had installed. Surpris ...

encountering a problem with permissions while attempting to update npm

Has anyone encountered a permission error with npm when trying to update to the latest version? I recently tried updating npm and received this error message. I'm unsure of how to resolve it. Any suggestions? marshalls-MacBook-Air:Desktop marshall$ n ...

Encountering a build time issue while incorporating Redis into Next.js

Incorporating Redis into my Next.js project has been beneficial overall. However, during the build process (next build), an error arises when it attempts to connect to Redis, resulting in the following message: Collecting page data ..[ioredis] Unhandled e ...