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:
JavaScript
x
51
51
1
{
2
"code": 79,
3
"codeName": "UnknownReplWriteConcern",
4
"errInfo": {
5
"writeConcern": {
6
"w": "majority;",
7
"wtimeout": 0,
8
"provenance": "clientSupplied"
9
}
10
},
11
"result": {
12
"n": 1,
13
"opTime": {
14
"ts": {
15
"$timestamp": "7022899934215012355"
16
},
17
"t": 99
18
},
19
"electionId": "7fffffff0000000000000063",
20
"ok": 1,
21
"writeConcernError": {
22
"code": 79,
23
"codeName": "UnknownReplWriteConcern",
24
"errmsg": "No write concern mode named 'majority;' found in replica set configuration",
25
"errInfo": {
26
"writeConcern": {
27
"w": "majority;",
28
"wtimeout": 0,
29
"provenance": "clientSupplied"
30
}
31
}
32
},
33
"$clusterTime": {
34
"clusterTime": {
35
"$timestamp": "7022899934215012355"
36
},
37
"signature": {
38
"hash": "/gnrM/bYkyRYi4XXXmEnkaLJJpg=",
39
"keyId": {
40
"low": 1,
41
"high": 1620408145,
42
"unsigned": false
43
}
44
}
45
},
46
"operationTime": {
47
"$timestamp": "7022899934215012355"
48
}
49
}
50
}
51
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:
JavaScript
1
71
71
1
const router = require("express").Router();
2
const User = require("../models/User");
3
const Post = require("../models/Post");
4
5
//CREATE POST
6
router.post("/", async (req, res) => {
7
const newPost = new Post(req.body);
8
try {
9
const savedPost = await newPost.save();
10
res.status(200).json(savedPost);
11
} catch (err) {
12
res.status(500).json(err);
13
}
14
});
15
16
//UPDATE POST
17
router.put("/:id", async (req, res) => {
18
try {
19
const post = await Post.findById(req.params.id);
20
if (post.username === req.body.username) {
21
try {
22
const updatedPost = await Post.findByIdAndUpdate(
23
req.params.id,
24
{
25
$set: req.body,
26
},
27
{ new: true }
28
);
29
res.status(200).json(updatedPost);
30
} catch(err) {
31
res.status(500).json(err);
32
}
33
} else {
34
res.status(401).json("You can only update your post.")
35
}
36
} catch(err) {
37
res.status(500).json(err)
38
}
39
});
40
41
//DELETE POST
42
router.delete("/:id", async (req, res) => {
43
try {
44
const post = await Post.findById(req.params.id);
45
if (post.username === req.body.username) {
46
try {
47
await post.delete();
48
res.status(200).json("Post has been deleted...");
49
} catch (err) {
50
res.status(500).json(err);
51
}
52
} else {
53
res.status(401).json("You can delete only your post!");
54
}
55
} catch (err) {
56
res.status(500).json(err);
57
}
58
});
59
60
//GET USER
61
router.get("/:id", async (req, res) => {
62
try {
63
const post = await Post.findById(req.params.id);
64
res.status(200).json(post);
65
} catch (err) {
66
res.status(500).json(err);
67
}
68
});
69
70
module.exports = router;
71
Advertisement
Answer
Just Try this :
Check the method type and the link route in Postman.
JavaScript
1
9
1
router.delete("/:id", async (req, res) => {
2
try {
3
const deletedPost = await Post.findByIdAndRemove(req.params.id);
4
res.status(200).json({ message: "Operation Succeded", deletedPost });
5
} catch (err) {
6
res.status(500).json({ message: "No Data to Delete.",err });
7
}
8
});
9