Here's a code snippet I am working with:
let result = null;
await fetch(request)
.then((response) => {
if (response.status === 201) {
console.log("Success");
result = response.json();
} else {
throw new Error("Something went wrong on API server!");
}
})
.catch((error) => {
console.error(error);
});
console.log("Response:", await result);
});
});
The received output is as follows:
[0-0] Success [0-0] Response: { data: { results: [ [Object] ], total: 1, page: 1 }, error: null }
However, when checking the response in Postman, it appears like this:
{
"data": {
"results": [
{
"customerId": 123,
"firstName": "Name",
"lastName": "LastName",
"displayName": "Name LastName",
"phoneNumber": "4159436367",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c39313d35301c39313d3530723f3331">[email protected]</a>",
"notes": null,
"memberNumber": null,
"highlights": null
}
],
"total": 1,
"page": 1
},
"error": null
}
How can I format the array response to be displayed similar to how it appears in Postman? Thank you for any assistance.
I attempted to modify the code with this addition:
const parsedResponce = result;
const obj = JSON.parse(parsedResponce);
console.log(obj.phoneNumber);
This led to the following error:
SyntaxError: Unexpected token o in JSON at position 1