获取以下TypeError错误,当我尝试使用nodejs请求模块传递头信息时

时间:2015-04-28 07:52:15

标签: javascript node.js express request npm

exports.loginUser = function(req, res) {

    console.log(req.body.loginemail);
    console.log(req.body.loginpassword);

    var options = {
        uri: webServiceURL.login,
          method: "POST",
        body: {
            "login": req.body.loginemail,
            "password": req.body.loginpassword
        },
        json: true,
        headers: req.headers
    };
    request(options, function(error, response, body) {

        console.log(body);

    });

};

当我尝试将标头信息传递给请求模块时,我收到以下错误。基本上我想要实现的是将cookie信息传递给API调用:

TypeError: undefined is not a function
   at rfc3986 (E:\nodeui\webui\node_modules\request\request.js:234:14)
   at Request.json (E:\nodeui\webui\node_modules\request\request.js:1357:21)
   at Request.init (E:\nodeui\webui\node_modules\request\request.js:599:10)
   at new Request (E:\nodeui\webui\node_modules\request\request.js:272:8)
   at request (E:\nodeui\webui\node_modules\request\index.js:56:10)
   at exports.loginUser (E:\nodeui\webui\app\controllers\user.js:27:6)
   at Layer.handle [as handle_request] (E:\nodeui\webui\node_modules\express\lib\router\layer.js:82:5)
   at next (E:\nodeui\webui\node_modules\express\lib\router\route.js:110:13)
   at Route.dispatch (E:\nodeui\webui\node_modules\express\lib\router\route.js:91:3)
   at Layer.handle [as handle_request] (E:\nodeui\webui\node_modules\express\lib\router\layer.js:82:5)

1 个答案:

答案 0 :(得分:0)

exports.loginUser = function(req, res) {

    console.log(req.body.loginemail);
    console.log(req.body.loginpassword);

    var options = {
        uri: webServiceURL.login,
        method: "POST",
        body: JSON.stringify({
            "login": req.body.loginemail,
            "password": req.body.loginpassword
        }),
        json: true,
        headers: req.headers
    };
    request(options, function(error, response, body) {

        console.log(body);

    });

};

你必须改变

body : {"login": req.body.loginemail,"password": req.body.loginpassword}

body : JSON.stringify({"login": req.body.loginemail,"password": req.body.loginpassword})

尝试并评论结果