0

I'm getting a weird warning that I'm Expected to return a value in arrow function in the following code

export const loginUser = userData => dispatch => {
    axios.get("http://localhost:5000/users")
        .then((res)=>{
            res.data.map(user => {                     <---- warning occurs here
                if(user.email === userData.email){
                    if(user.password === userData.password){
                        localStorage.setItem("user", JSON.stringify(user));
                        dispatch(setCurrentUser(user));
                    }
                }
            })
        })
        .catch((err)=>dispatch({
            type: GET_ERRORS,
            payload: err.data
        }))
}

But I thought it was not required to declare an explicit return in an arrow function, what am I doing wrong? How can I fix this?

Nimrod
  • 375
  • 1
  • 12
  • Does this answer your question? [JavaScript: Difference between .forEach() and .map()](https://stackoverflow.com/questions/34426458/javascript-difference-between-foreach-and-map) – idmean Jan 23 '21 at 07:02
  • No, I'm having doubts on the arrow function – Nimrod Jan 23 '21 at 07:03
  • @Nimrod It doesn't need explicit return if it it is a single line statement. – Nike Lepz Jan 23 '21 at 07:05
  • try this `export const loginUser = (userData,dispatch) => ` – zahra zamani Jan 23 '21 at 07:06
  • No, I'm still getting the warning – Nimrod Jan 23 '21 at 07:10
  • https://stackoverflow.com/questions/45014094/how-do-i-fix-expected-to-return-a-value-at-the-end-of-arrow-function-warning – zahra zamani Jan 23 '21 at 07:14
  • add return `return dispatch(setCurrentUser(user));` or just return anything like `return user` – Thanh Jan 23 '21 at 07:14
  • you should be using a `forEach` instead of `map`, there is no point to use `map` there since you don't plan to return anything. – buzatto Jan 23 '21 at 07:16
  • Block bodied arrow functions require the `return` keyword to produce a value. – Aluan Haddad Jan 23 '21 at 07:29
  • thanks, ```forEach``` removed the warning but is catching the error. I don't know why – Nimrod Jan 23 '21 at 07:31
  • The warning went away because the result of your previous `map()` would have been `[undefined,undefined,undefined ...]` which is very unlikely what any sane programmer wants so the linter complains about it. Foreach, unlike map, does not return a result so there is nothing wrong if you don't return anything to `forEach()`. – slebetman Jan 23 '21 at 07:48

1 Answers1

3

For quick example when you are using map like this

const map1 = array1.map(x => x * 2);

you no need to use an explicit return

but in scenario

const map1 = array1.map(x => {
return x*2
});

the main difference is due to flower brackets which expect a return statement.

The code you provided is doing dispatch action in this case it doesn't require anything to return, I would suggest using foreach loop [UPDATED]

export const loginUser = userData => dispatch => {
    return axios.get("http://localhost:5000/users")
        .then((res)=>{
            res.data.foreach(user => {                     <---- warning occurs here
                if(user.email === userData.email){
                    if(user.password === userData.password){
                        localStorage.setItem("user", JSON.stringify(user));
                        dispatch(setCurrentUser(user));
                    }
                }
            })
        })
        .catch((err)=>dispatch({
            type: GET_ERRORS,
            payload: err.data
        }))
}
akhil
  • 1,649
  • 3
  • 19
  • 31
  • Shouldn't you `return axios.get(...` so that callers can track the operation and handle errors? – Aluan Haddad Jan 23 '21 at 07:31
  • @AluanHaddad yes we can do it but, in question it is pointed that Nimrod is facing warning at this statement res.data.map(user => { – akhil Jan 23 '21 at 07:33
  • I will also update my answer thanks for pointing @AluanHaddad – akhil Jan 23 '21 at 07:34
  • 1
    Right, but it's a good idea to forward the promise and it fits with the premise of your answers theme – Aluan Haddad Jan 23 '21 at 07:34
  • @akhil using ```foreach``` removed the warning, but is catching the error, how could that happen? – Nimrod Jan 23 '21 at 07:36
  • @Nimrod you mean you are getting error in catch block? – akhil Jan 23 '21 at 07:40
  • Yes, how's that happening? – Nimrod Jan 23 '21 at 07:43
  • Is it because the promise is not being resolved, because nothing is being returned? – Nimrod Jan 23 '21 at 07:46
  • may be you are correct @Nimrod, can you try this one .catch((err)=>{dispatch({ type: GET_ERRORS, payload: err.data }) return Promise.reject() }) or .catch((err)=>{dispatch({ type: GET_ERRORS, payload: err.data }) return err; }) – akhil Jan 23 '21 at 07:52
  • The answer uses `foreach` (lowercase "e") instead of `forEach` (uppercase "E"), maybe that causes an error? Btw the two `if`s can be combined with `&&`. – Matias Kinnunen Jan 23 '21 at 13:46