2016-03-29 27 views
2

node.jsプロセス(クライアント)から別の(サーバー)に配列を送信しようとしています。node.js request POST配列 "最初の引数は文字列またはバッファでなければなりません"

「クライアント」のNode.jsのマイコード:

var express  = require('express'); 
var app   = express(); 
var config = require('./config'); 
var bodyParser  = require('body-parser'); 
var methodOverride = require('method-override'); 
var request  = require('request'); 

app.set('port', process.env.PORT || 3009); 
app.use(bodyParser.json()); // parse application/json 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json 
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded 

app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT 

var arr = [{ 
     date : "2016/1/26", 
     count: 6 
    }, 
       { 
     date : "2016/1/27", 
     count: 0 
    }, 
    { 
     date : "2016/1/28", 
     count: 0 
    }, 
    { 
     date : "2016/1/29", 
     count: 0 
    }, 
    { 
     date : "2016/1/30", 
     count: 0 
    }, 
    { 
     date : "2016/1/31", 
     count: 2 
    }, 
    { 
     date : "2016/2/1", 
     count: 0 
    }, 
    { 
     date : "2016/2/2", 
     count: 4 
    }]; 


     request.post({ 
      uri: config.URL, 
      headers: { 
       'Content-Type':'application/json;charset=UTF-8', 
       'Accept-Encoding':'gzip, deflate', 
       'X-Requested-With': 'XMLHttpRequest', 
       'Accept':'application/json, text/plain, */*', 
       'User-Agent': 'UserAgent' 
      }, 
      body: arr 
      }, function(err, res, body){ 

        //whatever 
      }); 

サーバー側で私は情報を受信し、それをCONSOLE.LOG。コードを起動すると

、私はクライアントに取得しています:TypeError('first argument must be a string, Array, or Buffer');

私は配列を送信できないのはなぜ? body: JSON.stringify(arr)とサーバーに、私は戻って、このような配列にそれを解析しよう:私はこのようなクライアントでは、配列を文字列化した場合、私は...私は1000倍それを行っている


を誓うvar data = JSON.parse(req.body);私は、次の取得しますサーバーでエラーデータを解析:

SyntaxError: Unexpected token o 
    at Object.parse (native) 
    at exports.uploadReads (C:\node\stockare2\server\companys\companys.controller.js:628:21) 
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5) 
    at next (C:\node\stockare2\node_modules\express\lib\router\route.js:131:13) 
    at uploadUser (C:\node\stockare2\server\companys\companys.routes.js:117:7) 
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5) 
    at next (C:\node\stockare2\node_modules\express\lib\router\route.js:131:13) 
    at Route.dispatch (C:\node\stockare2\node_modules\express\lib\router\route.js:112:3) 
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5) 
    at C:\node\stockare2\node_modules\express\lib\router\index.js:277:22 
    at Function.process_params (C:\node\stockare2\node_modules\express\lib\router\index.js:330:12) 
    at next (C:\node\stockare2\node_modules\express\lib\router\index.js:271:10) 
    at serveStatic (C:\node\stockare2\node_modules\express\node_modules\serve-static\index.js:74:16) 
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5) 
    at trim_prefix (C:\node\stockare2\node_modules\express\lib\router\index.js:312:13) 
    at C:\node\stockare2\node_modules\express\lib\router\index.js:280:7 

私はデータを解析しようとする前にreq.bodyをCONSOLE.LOGた場合、私はデータが奇妙な形式になって見ることができます:

{ '{"date":"2016/1/26...... 

答えて

2

要求にオプションjson: trueを追加しよう:

request.post({ 
     uri: config.URL, 
     headers: { 
      'Content-Type':'application/json;charset=UTF-8', 
      'Accept-Encoding':'gzip, deflate', 
      'X-Requested-With': 'XMLHttpRequest', 
      'Accept':'application/json, text/plain, */*', 
      'User-Agent': 'UserAgent' 
     }, 
     json: true, 
     body: arr 
     }, function(err, res, body){ 
0

使用var data = eval(req.body) javascriptオブジェクトに変換します。

+0

特に、リクエストボディにエンドユーザによって生成されたものが含まれている場合は、eval()から離れてください。 ref:https://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil –

関連する問題