score:1
Accepted answer
inside your fetchutil
function, it ends with no return value, which means your fetchutil
function is going to implicitly return undefined
.
you said
fetch(`${process.env.react_app_api}${url}`, request)
.then(response => {
if(response.ok) {
return response.json()
} else if(response.status === 401) {
refreshtoken().then(() => retryfn())
} else {
console.error(`error on fetching data from api: ${response.status}, ${response.text}`)
}
})
.then(json => {
console.log(json) // (1)
return json
})
.catch(error => console.error(error))
inside this function, (1)
part works well, right?
i think if you change your code like below it would work.
first, update your fetchutil
code like this. return fetch.
const fetchutil = (url, requestmethod, requestbody, retryfn) => {
// block thread if the token needs to be refetched
if(sessionstorage.getitem('token') == null || number(sessionstorage.getitem('token_expiration')) < new date().gettime()) {
refreshtoken()
}
let request = {
method: requestmethod,
headers: {
'authorization': `bearer ${sessionstorage.getitem('token')}`,
'content-type': 'application/json'
}
}
if(requestmethod === 'post' || requestmethod === 'put') {
request.body = json.stringify(requestbody)
}
// return fetch here! it will return a promise object.
return fetch(`${process.env.react_app_api}${url}`, request)
.then(response => {
if(response.ok) {
return response.json()
} else if(response.status === 401) {
refreshtoken().then(() => retryfn())
} else {
console.error(`error on fetching data from api: ${response.status}, ${response.text}`)
}
})
.then(json => {
console.log(json)
return json
})
.catch(error => console.error(error))
}
second, update your getchargehistory
like this.
const getchargehistory = async (installationid) => {
const result = await fetchutil(`/chargehistory?options.installationid=${installationid}`, 'get', {}, getchargehistory)
console.log(result);
}
because i don't have full access to your code, there still might be errors left, but i hope this helps!
Source: stackoverflow.com
Related Query
- Promise resolving function returns undefined
- TypeError: Cannot read property 'then' of undefined although function returns promise
- Put function that returns promise in redux state
- React function returns undefined
- await function returns a promise and returns true even if the condition is false | React Functional Component
- Redux thunk not resolving before next action + then is not a function or dispatch undefined
- Function Undefined in Axios promise
- Axios get function returns a promise in React
- Why my debounce function returns undefined value?
- Javascript base64 encoding function returns undefined
- JS / React - Function returns undefined before finished
- React: async/await function returns undefined then value I want
- React - setTimeout inside promise flashes UI elements, randomly returns array undefined
- In my code I got undefined while function returns true
- How to get the return value of a async function that returns a promise
- Mock function with jest that contains a callback as an argument which returns a promise
- sending a parameter to a function returns Cannot read property 'props' of undefined
- ReactJS: await function for fetching data returns a pending promise
- async await function returns promise instead of string
- How to call a function that returns a promise from a different file
- setup function returns undefined when testing a custom hook usePrevious
- Testing ReactJS component methods with enzyme shallow - function returns undefined instead of true
- Resolving a fetch promise and passing result to a non-await function
- Filter Function returns undefined
- js function call returns undefined
- firestore function only returns a promise
- KeyCode returns undefined when function is bound to the class react
- Function runs properly when called however always returns undefined
- React imported function returns undefined
- Array.map function returns undefined or [undefined]
More Query from same tag
- useEffect is not firing inside Hoc
- How is the localStorage is used within react.js?
- Changing route parameters when a different dropdown option is selected in React
- Using Array.find() to try and dynamically grab a specific object value and return it based on Object name, nothing returning from function
- How to properly use React.creatRef()
- Reactjs component does not call componentWillMount method when route is matched
- react redux- updating state
- Serve React production App (local server) through Flask-Python
- How can we get the text field value during onSubmit method in latest react-hooks-form
- React Conditional Rendering
- How to test asynchronous action?
- Infinite scroll in React
- Getting type error while running react app, TypeError: instance.render is not a function
- NextJS Track Mounted Components in SSR
- How to change width of Mui DatePicker v5
- How to map on react JS with button?
- How to deploy react app in ubuntu server with bitbucket pipeline
- Unable to find svg element for unit-testing of d3 component in React using enzyme and cheerio
- I am not getting the list item in my React-app
- optional question mark and object destruction
- How to design a form with row and col span using antd grid?
- ReferenceError: privateSpecialRepair is not defined
- How can I css style a child react component?
- Using WebFontLoader with React Konva causes text offsets?
- How can I migrate my old js code to react?
- How to import props into a function that is being rendered in a component
- React Ref Form onSubmit Handler not triggered
- How do I re-route header menu items to a URL instead of internal file in React/Gatsby?
- Redux Thunk : redux thunk is not dispatching an action
- How can I attach a ref to a component that i am copying using React.cloneElement