【问题标题】:Why am I still getting an error with a DELETE request in Postman?为什么我仍然收到 Postman 中的 DELETE 请求错误?
【发布时间】:2022-01-25 17:13:34
【问题描述】:

我对编程有点陌生,我正在尝试向 Postman 发送删除请求,但我一直在 postman 中收到此错误。有人知道如何解决这个问题吗?

错误:

{
    "code": 79,
    "codeName": "UnknownReplWriteConcern",
    "errInfo": {
        "writeConcern": {
            "w": "majority;",
            "wtimeout": 0,
            "provenance": "clientSupplied"
        }
    },
    "result": {
        "n": 1,
        "opTime": {
            "ts": {
                "$timestamp": "7022899934215012355"
            },
            "t": 99
        },
        "electionId": "7fffffff0000000000000063",
        "ok": 1,
        "writeConcernError": {
            "code": 79,
            "codeName": "UnknownReplWriteConcern",
            "errmsg": "No write concern mode named 'majority;' found in replica set configuration",
            "errInfo": {
                "writeConcern": {
                    "w": "majority;",
                    "wtimeout": 0,
                    "provenance": "clientSupplied"
                }
            }
        },
        "$clusterTime": {
            "clusterTime": {
                "$timestamp": "7022899934215012355"
            },
            "signature": {
                "hash": "/gnrM/bYkyRYi4XXXmEnkaLJJpg=",
                "keyId": {
                    "low": 1,
                    "high": 1620408145,
                    "unsigned": false
                }
            }
        },
        "operationTime": {
            "$timestamp": "7022899934215012355"
        }
    }
}

现在,删除请求工作正常,因为我可以看到在发送删除请求时查询选择被删除,但我仍然在邮递员中收到该错误。我尝试使用这个解决方案https://stackoverflow.com/a/69779799/16216414,当我在 Postman 中使用任何其他请求时它工作正常。我试着检查我的代码

posts.js:

const router = require("express").Router();
const User = require("../models/User");
const Post = require("../models/Post");

//CREATE POST
router.post("/", async (req, res) => {
  const newPost = new Post(req.body);
  try {
    const savedPost = await newPost.save();
    res.status(200).json(savedPost);
  } catch (err) {
    res.status(500).json(err);
  }
});

//UPDATE POST
router.put("/:id", async (req, res) => {
  try {
    const post = await Post.findById(req.params.id);
    if (post.username === req.body.username) {
      try {
        const updatedPost = await Post.findByIdAndUpdate(
          req.params.id,
          {
            $set: req.body,
          },
          { new: true }
        );
        res.status(200).json(updatedPost);
      } catch(err) {
      res.status(500).json(err);
      }
    } else {
      res.status(401).json("You can only update your post.")
    }
  } catch(err) {
      res.status(500).json(err)
  }
});

//DELETE POST
router.delete("/:id", async (req, res) => {
  try {
    const post = await Post.findById(req.params.id);
    if (post.username === req.body.username) {
      try {
        await post.delete();
        res.status(200).json("Post has been deleted...");
      } catch (err) {
        res.status(500).json(err);
      }
    } else {
      res.status(401).json("You can delete only your post!");
    }
  } catch (err) {
    res.status(500).json(err);
  }
});

//GET USER
router.get("/:id", async (req, res) => {
  try {
    const post = await Post.findById(req.params.id);
    res.status(200).json(post);
  } catch (err) {
    res.status(500).json(err);
  }
});

module.exports = router;

【问题讨论】:

    标签: javascript node.js mongodb postman


    【解决方案1】:

    试试这个:

    检查 Postman 中的方法类型和链接路由。

    router.delete("/:id", async (req, res) => {
      try {
        const deletedPost = await Post.findByIdAndRemove(req.params.id);
        res.status(200).json({ message: "Operation Succeded", deletedPost });
      } catch (err) {
        res.status(500).json({ message: "No Data to Delete.",err });
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      相关资源
      最近更新 更多