2017-05-04 107 views
0

我有我的路線問題,我想從req.body形式檢查一些數據Express.js req.body parametr陣列

我有途徑

app.post(`/${randomQuestionsArray[0].id}`, (req, res) => { 
const reqValue = req.body.answer; 
console.log(reqValue); 
console.log(randomQuestionsArray[0].reqValue); 

}),

片段數據:

const jsonContent = { 
    "questions": [ 
     { 
      "id": "1", 
      'slug': "566fghg6", 
      "title": "post title 1", 
      "desc": "This is a test desc 1", 
      "answer1": { 
       "answer": "test1", 
       "value": true 
      }, 
      "answer2": { 
       "answer": "test2", 
       "value": false 
      }, 
      "answer3": { 
       "answer": "test3", 
       "value": false 
      }, 
      "answer4": { 
       "answer": "test4", 
       "value": false 
      } 
     }, 

req.body.answer - 給我表格值等ANSWER1
但我想從陣列
需要一些數據,我創建變量:

const reqValue = req.body.answer; randomQuestionsArray[0].reqValue give me undefined
但如果我寫的手: randomQuestionsArray [0] .answer1給我數據 「答案」:「測試1」, 「值」:真


我怎樣才能把req.body我陣列PARAMS?

+0

使用'randomQuestionsArray [0] [reqValue]'而不是' randomQuestionsArray [0] .reqValue'如果使用硬編碼值適合你。 –

+0

Thx尋求幫助。這是工作 :) –

回答

0

如果使用常規的POST方法您可以通過以下方式解決這一

比方說,你張貼到 「/數據記錄器」

var bodyParser = require('body-parser'); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json()); 

app.post("/datalogger", function (req, res) { 
    var strTitle = req.body.slug 
    console.log("The title: " + strSearch); 
    res.end("ok"); 
});