I am trying to access an element in the following json response from an API call via
foilResponse.data.products.edges[0].totalInventory
however no matter what variation of the above I use everything shows as undefined. When I query the endpoint with postman i get the full array returned, is there something I need to configure within Node?
{
data: { products: { edges: [Array] } },
extensions: {
cost: {
requestedQueryCost: 3,
actualQueryCost: 3,
throttleStatus: [Object]
}
}
2
hmm, it may be thats its failing to infer the type definition. try:
interface IFoilResponse {
data: { products: { edges: Record<string, any>[] };
extensions: {
cost: {
requestedQueryCost: number;
actualQueryCost: number;
throttelStatus: Record<string, any>;
}
}
const foilResponse: IFoilResponse = data;
console.log(foilResponse.data.products.edges[0].totalInventory);
Though, assuming you are not using typescript and are using javascript you code try:
(foilResponse.data.products.edges[0] as Record<string, any>)?.totalInventory;
though a cleaner way would be:
Object(foilResponse.data.products.edges[0])?.totalInventory;
sorry, for the rapid fire approach but, I figure that a few options would be better. though, looks like you’ve asked this question before here on stackOverflow but, looks the data can be accessed adding the node
property ex: foilResponse.data.products.edges[0].node.totalInventory