Reputation: 760
I am getting the warning 'expected to receive a value at the end of arrow function' once I call foreach function to go thru each child and update the record that matches.
var email = req.body.email;
var name = req.body.name;
var result = false;
userRef.once('value', snapshot => {
result = snapshot.forEach(child => {
if (child.val().email === email){
console.log(child.key);
admin.database().ref('users/'+child.key).update({name: name}).then(result =>{
res.status(200).json({message: 'Updated'});
return true;
})
.catch(err => {
console.log(err);
return false;
});
return true;
}
});
if (result === false){
res.status(200).json({message: 'User does not exist'});
}
});
I receveid from http post the parameter email, now I use 'foreach' to search for the user that has such email. Once I find it, I update its name with the other parameter 'name' and I return true to get out the foreach loop. However I receive a warning message about I am missing one return, which is in the case the code never gets into the if statemente. In a normal situation I would take the length of my array and I would return false once the loop reaches its end but here I can't.
My database:
users
-Lbq98URniAej2TkWBhG
email: "[email protected]"
name: "Pepe"
-Lbq9GC1A131De-iumI0
email: "[email protected]"
name: "Pipa10"
Upvotes: 0
Views: 398
Reputation: 317467
You didn't return a boolean value from every code path in your callback. For example, what if child.val().email === email
evaluates false? What does the callback return? In your code, it doesn't return anything, because no return statement was executed. That's what's causing the error message. You need to return something in that case. Perhaps something like this will work:
result = snapshot.forEach(child => {
if (child.val().email === email){
console.log(child.key);
admin.database().ref('users/'+child.key).update({name: name}).then(result =>{
res.status(200).json({message: 'Updated'});
return true;
})
.catch(err => {
console.log(err);
return false;
});
// this line doesn't help anything, commented out
//return true;
}
return true; // this line always returns true from forEach
});
Upvotes: 1