javascript - Promise based Action succeeds, data gets resolved but i get an error message -


so i'm trying build action on react , need promise. action succeeds, receive response server error saying:

vm89852:98 uncaught typeerror: cannot read property 'then' of undefined. 

action:

export const fetchallaccounts = (token, dispatch) => {  return new promise((resolve, reject) => {   fetchaccountsstart((dispatch));    return axios.get(`${api_url}/accounts`, {    headers: {      'authorization': `bearer ${token}`,      'content-type': 'application/json'     }    }).then(       (response) => {         fetchaccountssuccess(dispatch, response.data);         resolve(response.data);       },(error) => {         fetchaccountsfailed(dispatch, error.data);         reject(error.data);       },    );  }); }; 

also heres method on how call action.

 this.props.fetchallaccounts(token)   .then((data) => {     console.log("#".repeat(120));     console.log(data);     console.log("#".repeat(120));   }).catch((error) => {     console.log("#".repeat(120));     console.log(error);     console.log("#".repeat(120));   }); 

your comment

heres call mapdispatchtoprops ...
fetchallaccounts: (token) => { fetchallaccounts(token, dispatch) },

there problem, in comment. either needs be

fetchallaccounts: (token) => { return fetchallaccounts(token, dispatch) }, 

or

fetchallaccounts: (token) => fetchallaccounts(token, dispatch), 

understand arrow functions, if use {} need return, there no implied return

as bonus - remove promise constructor anti-pattern

export const fetchallaccounts = (token, dispatch) => {     fetchaccountsstart((dispatch));     return axios.get(`${api_url}/accounts`, {         headers: {             'authorization': `bearer ${token}`,             'content-type': 'application/json'         }     }).then(         (response) => {             fetchaccountssuccess(dispatch, response.data);             return response.data;         }, (error) => {             fetchaccountsfailed(dispatch, error.data);             throw error.data;             // borrowed @t.j.crowder's pastebin :p             // note it's best reject or throw error instance,             // not other types; e.g., `throw new error(error.data)` or             // `return promise.reject(new error(error.data))`          },     ); }; 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -