set json with request in Node.JS

To wait request get a response, use callback function. Compare below codes. I do not figure out but res.json cannot be a parameter.

OK, this works.

router.get('/', function(req, res) {
    setJson(res);
})

function setJson(url, id, callback) {
    request({url: your_url, json: true}, function(err, res, json) {
        callback.json(json);
    })
}

---------
No, this doesn't work.

router.get('/', function(req, res) {
    setJson(res.json);
})

function setJson(url, id, callback) {
    request({url: your_url, json: true}, function(err, res, json) {
        callback(json);
    })
}


コメント

人気の投稿