我正在使用 request
包发出 GET 请求。我试图将数据从请求传递到我的模板作为回调,但我不断收到错误 res.render is not a function
。这是我的代码:
module.exports = function (req, res) {
request("http://localhost:3000/api/employee", function(err, res, body) {
var apiResponse = JSON.parse(body);
var context = {
name: apiResponse[0].first_name
}
res.render('../views/home', context);
});
};
有人可以帮忙吗?
提前致谢!
最佳答案
module.exports = function (req, res) {
request("http://localhost:3000/api/employee", function(err, response, body) {
var apiResponse = JSON.parse(body);
var context = {
name: apiResponse[0].first_name
}
res.render('../views/home', context);
});
};
请使用此代码。
您代码中的问题是有两个 res
变量,res.render
应使用第一个 res
。
关于javascript - 将数据从请求对象传递到模板node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34486348/