How to fix Parsing error: Can not use keyword 'await' outside an async function
10,302
You need to have async
keyword for the inner
function
export function getData() {
return async (dispatch) => {
try {
const data = await API.graphql(graphqlOperation(query))
dispatch({ type: "DATA_AVAILABLE", data: data.data.listData.items });
} catch(err) {
console.log('error: ', err)
}
}
}
Related videos on Youtube
Author by
Skate to Eat
Updated on June 04, 2022Comments
-
Skate to Eat almost 2 years
Getting a error message saying
Parsing error: Can not use keyword 'await' outside an async function
from below code for Redux action.What is the proper way to write this?
export async function getData() { return (dispatch) => { try { const data = await API.graphql(graphqlOperation(query)) dispatch({ type: "DATA_AVAILABLE", data: data.data.listData.items }); } catch(err) { console.log('error: ', err) } } }
-
CertainPerformance almost 5 years
return (dispatch) => {
is notasync
-
AZ_ almost 5 years
return async (dispatch) => {
no need of async inexport async function getData()
-
Skate to Eat almost 5 years@CertainPerformance also tried
export const getUserJournals = () => async (dispatch) => { ... }
but didn't work :(
-
-
Skate to Eat almost 5 yearsThank you for the comment! the error message went away but still doesn't get the data from API...
-
Skate to Eat almost 5 yearsGetting
error: TypeError: dispatch is not a function
-
Radonirina Maminiaina almost 5 yearsUpdated my explaination
-
Radonirina Maminiaina almost 5 yearsUpdated my post
-
Skate to Eat almost 5 yearsGetting
Uncaught (in promise) TypeError: dispatch is not a function
this time. -
Radonirina Maminiaina almost 5 yearsBut you should pass dispatch as fucntion
-
Skate to Eat almost 5 yearsIt actually seems to be working when it on my dev environment with AWS amplify but does not working on my local. Not sure why lol
-
Shubham Khatri almost 5 yearsIt may so be the case that qraphql query isn't working correctly in your local
-
Skate to Eat almost 5 yearsHave any idea to debug that on my local?
-
Skate to Eat almost 5 yearsThis works! I had an issue somewhere else. Thank you for the answer.