javascript - 将数据从请求对象传递到模板node.js

标签 javascript node.js controller request

我正在使用 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/

相关文章:

node.js - NPM 的可解析输出

javascript - 在 Twitter API 中使用 since_id 和 max_id

javascript - NiceScroll 不适用于 Html

node.js - Nodejs promise 类型错误 : Cannot read property 'then' of undefined

node.js - heroku 中的 Angular2 路由

controller - 在 Twig 中渲染 Controller - 意外的 "render"标签 - 期望定义的 "block"标签的结束标签

java - 编写 Controller 的正确方法

Grails:使用一个 Controller 操作同时渲染两个 View

javascript - 使用 JavaScript 提交表单?

javascript - React router dom 导航方法无法正常工作