score:12
Accepted answer
your await
, when erroring out returns undefined
, since console.log
in your catch
returns undefined
, and the return value is inferred as void
.
basically here you are mixing async/await sync style with promise async style of writing async code, it's better to stick to one to avoid confusion like here with good'ol try catch
.
so it can go like this:
const fetchitem = async() => {
try {
const response = await axios("http://localhost:4000/items?id=2");
console.log(response.data);
} catch (error) {
console.log(error)
}
};
score:1
would comment, but can't yet...
the inner catch
can return a possible void
. if you remove that/handle it different then at least ts doesn't complain.
Source: stackoverflow.com
Related Query
- Cannot access data property of Axios response object after successful GET request: Property 'data' does not exist on type 'void'
- Cannot assign to read only property 'approved_by_customer_admin' of object '#<Object>' after updating the api data manually
- react axios get request unable to pass/access response data
- Cannot read property 'map' of undefined - Cant map data from axios request
- Axios get method response in React cannot be displayed getting data from firebase as an array in my blog application
- Axios first get the response and then render form TypeError: Cannot read property '***' of undefined
- How to extract data from axios GET request object in React?
- update the state of my component with the response data after Post request with axios
- Cannot read value from axios GET request response outside of axios function
- tried to access the data via axios but Response to preflight request doesn't pass access control check
- Axios is unable to get the response data after posting
- React component returning cannot read property '__reactInternalInstance$ of null after trying to access array
- How do I access data returned from an axios get to display on a web page using react js?
- Get axios Response object when error occurred with Redux Saga
- TypeError: Cannot read property 'data' of undefined - can't access Object "props" beyond certain level in Reactjs
- axios get request for real-time data
- React JS & Axios Render after getting response data
- How to get a data after react select using axios and ReactJS?
- JS Fetch - cannot get data from BadRequest response
- Getting empty object in axios GET request
- Using axios response data in another axios request
- Cannot read property 'then' of undefined - Axios get in React
- Axios get request response with 403 error forbidden
- Returning promise object on GET API request by Redux and Axios
- How to get axios post data request on nodeJs
- Accessing data from Axios GET request
- Axios GET request for form encoded data
- How to access data from get request made by a React functional component to localhost server using Axios?
- Axios promise resolving on pre-flight request response which makes associated GET execute out of order with rest of app
- Cant use dot or bracket notation to access keys or data in json/javascript object saved to state after fetch
More Query from same tag
- Proper way to load local stored images from folder in React
- Property in Component State not updating quick enough
- props.history.push inside functional component renders doesn't render the component
- Question about React.useEffect() and React.useState()
- Using Map() function to pull out double nested JSON data with ReactJS
- How to map an index range in react?
- Module '"<file path>/react-redux"' has no exported member 'Dispatch'
- In Flux what is responsible for direct talking to API
- React - filter nested array and update the state
- Audio play() not responding in create-react-app componentDidMount
- Dynamically imported modules without relative paths
- NextJS/Django Manually Typing in URLs
- How to convert a UTC date to "MM/DD/YYYY" for react-datepicker?
- Next.JS - Access `localStorage` before rendering page
- Using <ContextProvider> inside of <Switch>
- How to load a new component on button click in ReactJs?
- cookie and security in react application
- How to check object in React for null or empty or undefined?
- why axios return promise error in reactjs
- Input data of a PATCH request with formData and Axios is unavailable on Laravel
- ReactJS .NET not able to set cookie
- How to turn React Component able to receive variables
- React JS loading FB share
- Reactjs - Assigning json response to default array
- Get multiple attributes from a single selector in Puppeteer
- How do I solve 'Object possibly null' error in TS?
- How to properly test promises
- On API Call, I want to display the data in table
- Chart.js Scale Y- axis ticks text alignment center
- How do I get img to align at flex-end?