I am quite new to javascript and I am trying to understand the function expression as argument for other function call.
Here is one example that use mongoose to do a search:
app.get('/auth/:id', (req, res) => {
var db = req.db;
User.findById(req.params.id, function(err, user){
if (err){
res.send(err)
}
res.send({
success: true
})
})
})
I understand that function(err,user){...}
inside User.findById()
is a function expression, and if the User.findById()
return something, it will assign value to variable user
, as well as (if) error message to err
.
The part that confuse me is that if function findById is used in this way User.findById(param_1,param_2,...)
, then how err
and user
receive its value?