Why cant I inline call to res.json?

前端 未结 1 1933
梦如初夏
梦如初夏 2020-11-27 07:50

I have and expressjs application and on a specific route I call a function that responds with a user from the database by calling res.json with the database doc

相关标签:
1条回答
  • 2020-11-27 08:11

    The json function loses its correct this binding when used like that since .then is going to invoke it directly without reference to the res parent object, so bind it:

    var getUserInline = function(req, res) {
        getUserFromDatabase().then(res.json.bind(res));    
    }
    
    0 讨论(0)
提交回复
热议问题