#missing propert from working api
1 messages · Page 1 of 1 (latest)
Based on the information provided in the extracts, it's not clear why the amount property would be undefined in your console log. The code you've shared seems correct and should work as expected.
However, there are a few things you could check:
-
Ensure that the
body.lengthin your API route is correctly calculating the length of an array and not returningundefined. Ifbody.lengthisundefined, thenamountwould also beundefined. -
Make sure that the
fetchrequest is successful and the response status is200. You can check this by loggingres.statusbefore you attempt to parse the JSON. -
Check if there's a network error or some other issue that might be causing the fetch request to fail. You can do this by wrapping your fetch request in a try-catch block and logging any errors that might occur.
Here's an example of how you can do this:
try {
const res = await fetch(`<http://localhost:3000/api/posts?page=${params.page}`>);
console.log(res.status); // Check the response status
const body : JSONResponse<Array<Post>>= await res.json();
console.log("b", body.amount);
} catch (error) {
console.error(error); // Log any errors
}
If none of these suggestions help, you might need to debug your code further or seek help from other resources or forums.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@naive lagoon kapa.ai is still learning and improving, please let us know how it did by reacting below
@marble monolith api works and it returns this 100 in browser but in code its undefined
Let me check the knowledge base for you. One moment! :books:
Based on the information provided in the extracts, it's not clear why the amount property would be undefined in your console log. The code you've shared seems correct and should work as expected.
However, there are a few things you could check:
-
Ensure that the
body.lengthin your API route is correctly calculating the length of an array and not returningundefined. Ifbody.lengthisundefined, thenamountwould also beundefined. -
Make sure that the
fetchrequest is successful and the response status is200. You can check this by loggingres.statusbefore you attempt to parse the JSON. -
Check if there's a network error or some other issue that might be causing the fetch request to fail. You can do this by wrapping your fetch request in a try-catch block and logging any errors that might occur.
Here's an example of how you can do this:
try {
const res = await fetch(`<http://localhost:3000/api/posts?page=${params.page}`>);
console.log(res.status); // Check the response status
const body : JSONResponse<Array<Post>>= await res.json();
console.log("b", body.amount);
} catch (error) {
console.error(error); // Log any errors
}