Express.js req.body parametr array

时间:2017-05-04 10:30:59

标签: arrays json node.js express routes

我的路线有问题,我想从req.body表格中查看一些数据

我有路线

  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 - 给我价值表格等回答1 但是我想从数组中获取一些数据 我创建变量:

const reqValue = req.body.answer; randomQuestionsArray[0].reqValue give me undefined
但如果我手写: randomQuestionsArray [0] .answer1给我数据  "回答":" test1",  "价值":真实


如何将req.body放入我的数组参数中?

1 个答案:

答案 0 :(得分:0)

如果您使用常规POST方法,您可以通过以下方式解决此问题

假设你发帖到“/ datalogger”

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");
});