I'm working with the below code.
Promise.all(STATUS_CODES.map((status) => {
const url = `https://${domainName}/api/v2/search/tickets?query="status:${status}"`;
return client.request.get(url, options);
})).then((responses) => { render(responses); }) // [1] Function invoked here
.catch((err) => {
showError('API request(s) failed.');
console.error('API request(s) failed.', err);
});
}
function render(responses) {
/** Convert JSON String into Javascript Object */
const data = responses.map(r => JSON.parse(r.response)); // [2] Confused with this line
const max = Math.max(...data.map(d => d.total));
The problem that I need help with is in this line
const data = responses.map(r => JSON.parse(r.response));
Here is some data that I fetch the JSON at ${status} = 2
: https://codebeautify.org/online-json-editor/cb535226
Problem
When [1] is invoked, and the JSON string is sent as a parameter to [2] to parse in JS Object. But, in the statement r.response
, I expect there has to be a response
property in the JSON. But I'm not able to find it if I manually search for it.
However in the next line d.total
, I'm able to find total
property.
The Complete code works just as fine as expected, but I want to understand how does it work if response
property is not there in the JSON String?
It sounds like client.request.get(url, options)
returns a Promise
that resolves to an object which has a .response
property. responses
is an array of those objects , not an array of JSON strings.
In other words, each JSON response is wrapped inside another object, and accessing the .response
property on one of those objects gives you access to the actual response data (which, here, is a JSON string).
The wrapper object (in your code, it's called r
) may contain other data like the URL requested, or the status code of the response. The .response
property is not part of the JSON - it's part of the client.request.get
interface.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.