【问题标题】:PUT/PATCH not going through when using postman + express使用邮递员 + 快递时 PUT/PATCH 未通过
【发布时间】:2020-03-16 11:16:39
【问题描述】:

我正在尝试更新用户的送货信息。我在 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);

【问题讨论】:

    标签: mongodb express mongoose postman


    【解决方案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!",
            });
          }
        });
    })
    

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 2017-03-08
      • 2016-09-17
      • 2015-08-26
      • 2021-01-15
      • 2018-12-20
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多