Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected to return a value in arrow; function array-callback-return. Why?

I'm having some issues understanding why I'm getting a compile warning on this piece of my react code

fetch('/users')
        .then(res => res.json())
        .then(data => {
            data.map(users => {
                console.log(users);
            });
        });

The warning I'm getting is Expected to return a value in arrow function array-callback-return

However I'm still get the json object values from my /users, and they are printed to the console individually. The object is:

    {
        id: 1,
        username: "Foo"
    }, {
        id: 2,
        username: "Bar"
    }

Am I missing a return statement, or am I missing something with how map returns values after a .then()? I'm unclear on why the compile warning would appear at all.

like image 911
Chef1075 Avatar asked Jan 09 '18 08:01

Chef1075


People also ask

Does Arrow function return value by default?

No, arrow functions do not have to return a value.

Do arrow functions automatically return?

Arrow functions allow you to have an implicit return: values are returned without having to use the return keyword. This should be the correct answer, albeit needing a bit more explanation. Basically when the function body is an expression, not a block, that expression's value is returned implicitly.


3 Answers

Your specific example is:

data.map(users => {
   console.log(users);
});

Where data is the following array:

[
  {id: 1, username: "Foo"},
  {id: 2, username: "Bar"},
]

data.map function (check Array.prototype.map specification) converts one array (data in your case) to a new array. The conversion (mapping) is defined by the argument of data.map, also called the callback function. In your case, the callback function is the arrow function users => {console.log(users);}. The callback function of data.map must return a value. By returning a value for each item of the array is how data.map defines the mapping.

But in your case the callback function does not return anything. Your intention is not to do any kind of mapping, but just to console.log. So in your case you can use data.forEach (Array.prototype.forEach) as you don't use data.map functionality.

NOTE: Also you should have singular (rather than plural) name for the parameter of the callback function: data.map(user => {console.log(user);}); as this parameter is set to the individual element from the old array.

like image 139
croraf Avatar answered Oct 02 '22 21:10

croraf


Try Changing map(() => {}) to map(() => ())

{} - Creates a code block that expects an explicit return statement.
With () - implicit return takes place.

like image 45
Mayuresh Srivastava Avatar answered Oct 02 '22 22:10

Mayuresh Srivastava


If you don't need to mutate the array and just do the console.log() you can do data.forEach() instead. It shouldn't give you the warning. Map expects you to return a value after you've transformed the array.

fetch('/users')
        .then(res => res.json())
        .then(data => {
            data.forEach(users => {
                console.log(users);
            });
        });
like image 19
Rodius Avatar answered Oct 02 '22 22:10

Rodius