#missing propert from working api

1 messages · Page 1 of 1 (latest)

marble monolithBOT
#

Hi @naive lagoon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

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:

  1. Ensure that the body.length in your API route is correctly calculating the length of an array and not returning undefined. If body.length is undefined, then amount would also be undefined.

  2. Make sure that the fetch request is successful and the response status is 200. You can check this by logging res.status before you attempt to parse the JSON.

  3. 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

naive lagoon
#

@marble monolith api works and it returns this 100 in browser but in code its undefined

marble monolithBOT
#

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:

  1. Ensure that the body.length in your API route is correctly calculating the length of an array and not returning undefined. If body.length is undefined, then amount would also be undefined.

  2. Make sure that the fetch request is successful and the response status is 200. You can check this by logging res.status before you attempt to parse the JSON.

  3. 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: