Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument error, options.body in node.js?

what is the problem i don't know please help to solve this. when i post data then below error shows on terminal. Attach is the code also.

Error: Argument error, options.body.     at Request.init (/usr/lib/nodejs/request/index.js:351:13)     at new Request (/usr/lib/nodejs/request/index.js:124:8)     at Object.request (/usr/lib/nodejs/request/index.js:1279:11)     at Request._callback (/var/www/html/nodeproject/helloworld/controllers/login.js:68:11)     at Request.self.callback (/usr/lib/nodejs/request/index.js:148:22)     at Request.EventEmitter.emit (events.js:98:17)     at Request.<anonymous> (/usr/lib/nodejs/request/index.js:896:14)     at Request.EventEmitter.emit (events.js:117:20)     at IncomingMessage.<anonymous> (/usr/lib/nodejs/request/index.js:847:12)     at IncomingMessage.EventEmitter.emit (events.js:117:20) 

below is my code here am posting data using json.

module.exports.controller = function(BASE) {          var token_array = new Array();           BASE.APP.get('/login', function(req, res)     {         res.render('pages/login');              //res.sendFile(__dirname + '/login.html');      });     BASE.APP.post("/login", BASE.urlEncodedParser, function(req, response)            {              var devicetoken = req.usersession.devicetoken;             var body = req.body;             req.usersession.username = body.username;                  console.log(req.body);                 var userData={                     "deviceToken" : devicetoken,                     "password" : body.pwd,                     "username" : body.username                 };                 var digest = BASE.utils.hmac("sha1", "A12AA418-1F28-4464-8B67-29CBD02BC45C-F048B14F-F3E3-4F97-A522-F2275A364A0E", JSON.stringify(userData));                 var postData = {                     "deviceToken" : devicetoken,                     "password"    : body.pwd,                     "username"    : body.username,                     "digest"      : digest                 };                  var sPostData = BASE.utils.base64encode(BASE.utils.base64encode(JSON.stringify(postData)));                 BASE.request({                     url: "http://example.com/authenticate/",                     method: "POST",                     headers: {                         "content-type": "application/json",                     },                     body: sPostData                 },                 function(err,result,body){                     //console.log(JSON.stringify(postData));                      var body = JSON.parse(body);                     req.usersession.token = body.token;                      /*********************************has login permission***************************************/                     var loginData={                            "permission" : '08008749-F3A5-480B-A2B2-C21CEFED70F4',                         "token" : req.usersession.token                      };                        var logindigest = BASE.utils.hmac("sha1", "A12AA418-1F28-4464-8B67-29CBD02BC45C-F048B14F-F3E3-4F97-A522-F2275A364A0E", JSON.stringify(loginData));                     var loginpostData = {                                   "permission" : '08008749-F3A5-480B-A2B2-C21CEFED70F4',                                 "token" : req.usersession.token,                                 "digest"      :  logindigest                                    };                      BASE.request({                             url : "http://example.com/hasPermission/",                             method :"POST",                             headers : {                                          "content-type": "application/json",                                       },                             body :loginpostData                                 },                     function (err,result1,body) {                          console.log(body);                                });                           //response.redirect(301, '/contacts');                 });               });   } 

i am using same type code of another file what it should not give any error but this file show me

like image 706
Sumit Aggarwal Avatar asked Feb 24 '16 09:02

Sumit Aggarwal


1 Answers

Try adding json: true in your request options.

For example:

BASE.request({     url : "http://example.com/hasPermission/",     method :"POST",     headers : {         "content-type": "application/json",     },     body: loginpostData,     json: true }, 
like image 181
nadavvadan Avatar answered Oct 04 '22 07:10

nadavvadan