Home > Blockchain >  Why am I still getting an error with a DELETE request in Postman?
Why am I still getting an error with a DELETE request in Postman?

Time:12-26

I'm kinda new to programming and I am trying to send a delete request to Postman but I keep getting this error in postman. Would anyone know how to fix this?

ERROR:

{
    "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"
        }
    }
}

NOW, the delete request is working properly as I can see the query selection being deleted when I send the delete request but I am still getting that error in postman. I tried using this solution https://stackoverflow.com/a/69779799/16216414 which was working fine when I used any other request in Postman. I tried checking my code for

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;

CodePudding user response:

Just Try this :

Check the method type and the link route in 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 });
  }
});
  • Related