0

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?

4

1 回答 1

0

原来我的问题是关于javascript中的回调。这是关于该主题的非常好的帖子:https ://codeburst.io/javascript-what-the-heck-is-a-callback-aba4da2deced

于 2019-06-22T22:29:35.740 回答