Node.js服务进行放置操作

时间:2018-09-26 10:58:15

标签: json node.js

你好,我想以JSON格式放置操作。可能是我的JSON解析错误,因此我无法更新Data。我的JSON正文数据是

[{
  "receiptNo": "21456",
  "rollno": 12201,
  "bankcode": 2,
  "userid": "rifat",
  "__v": "0"
}]

还有我的NodeJS代码:

router.put('/receiptmaster1/update/:receiptNo', function (req,res) {
  receipt_master1.updateMany({ 
    receiptNo: req.params.receiptNo 
  }, {
    receiptNo: req.body.receiptNo,
    rollno: req.body.rollno,
    bankcode : req.body.bankcode,
    userid: 'rifat',
    __v: "0"
  }).then(function(err) {
    res.send({
      success: true,
      message: "Updated Successfully Master2"
    });
  }).catch(err => {
    res.status(500).send({
      message: err.message || "Error while Updating Server Data"
    });
  });
});

尽管我收到了此回复,但我的数据没有更新

"success":true,"message":"Updated Successfully Master2"

请帮助。

1 个答案:

答案 0 :(得分:1)

您的JSON是数组中的单个对象。发送时,请将发布的格式更改为对象(不包含包装数组):

{
  "receiptNo": "21456",
  "rollno": 12201,
  "bankcode": 2,
  "userid": "rifat",
  "__v": "0"
}

或更改在服务器上的访问方式:

router.put('/receiptmaster1/update/:receiptNo', function (req,res) {
  receipt_master1.updateMany({ 
    receiptNo: req.params.receiptNo 
  }, {
    receiptNo: req.body[0].receiptNo,
    rollno: req.body[0].rollno,
    bankcode : req.body[0].bankcode,
    userid: 'rifat',
    __v: "0"
  }).then(function(err) {
    res.send({
      success: true,
      message: "Updated Successfully Master2"
    });
  }).catch(err => {
    res.status(500).send({
      message: err.message || "Error while Updating Server Data"
    });
  });
});

请注意从req.body.req.body[0].的变化