使用邮递员+快递时PUT / PATCH无法通过

时间:2019-11-20 16:44:59

标签: mongodb express mongoose postman

我正在尝试更新用户的运送信息。我在mongodb中有默认数据,试图用PATCH / PUT覆盖。现在,邮递员可以无限期地挂起。猫鼬模型具有值,我的else {}语句输出要提交的JSON对象,没有错误,但从未成功。感谢您的帮助。 邮递员

Could not get any response

app.js

app.put('/api/users/:id', (req, res) => {

  User.findByIdAndUpdate({ _id: req.params.id },

    {
      fullName: req.body.fullName,
      address1: req.body.address1,
      address2: req.body.address2,
      city: req.body.city,
      state: req.body.state,
      zip: req.body.zip
    }, function (err, docs) {
      if (err) res.json(err);

      else {
       // console.log(docs)
      }
    });
})

模型

 const mongoose = require("mongoose");

const shippingSchema = mongoose.Schema({
  fullName: { type: String},
  address1: { type: String },
  address2: { type: String },
  city: { type: String },
  state: { type: String},
  zip: { type: String}
});

module.exports = mongoose.model("Shipping", shippingSchema);

1 个答案:

答案 0 :(得分:1)

您忘记发送回复,因此请求永远不会完成。

app.put('/api/users/:id', (req, res) => {
  User.findByIdAndUpdate({ _id: req.params.id },
    {
      fullName: req.body.fullName,
      address1: req.body.address1,
      address2: req.body.address2,
      city: req.body.city,
      state: req.body.state,
      zip: req.body.zip
    }, function (err, docs) {
      if (err) res.json(err);

      else {
        // console.log(docs)

        // don't forget to send a response
        res.json({
          "message": "works!",
        });
      }
    });
})