fikus
fikus

Reputation: 37

cannot DELETE post (express mongoose postman)

I'm trying to do simple REST operations with Postman request on http://localhost:5000/posts/60c9b65463cdb804e4238257 but with DELETE I'm getting this error:

404 not found Cannot DELETE /posts/60c8e5a860b84f0013a1d9d7

I will be glad for any tips.

https://i.sstatic.net/PL9P8.jpg

// index.js

app.use(express.json());
app.use(cors());

app.use("/posts", postRoutes);

//routes.js

const routes = express.Router();

routes.get("/", getPosts);
routes.post("/", createPost);
routes.patch("/:id", updatePost);
router.delete("/:id", deletePost);
export default routes;

// controller.js

export const deletePost = async (req, res) => {
  const { id } = req.params;

  if (!mongoose.Types.ObjectId.isValid(id))
    return res.status(404).send(`No post with id: ${id}`);

  try {
    await Bootcamp.findByIdAndDelete(id);

    res.json({ message: "Post deleted successfully.",success:true });
  } catch (error) {
    res.status(500).json({ success: flase, message: error.message });
  }
};

export default router;

Upvotes: 0

Views: 705

Answers (1)

Alamin
Alamin

Reputation: 2165

You have missed a "s" in your delete router line.


const routes = express.Router();

routes.get("/", getPosts);
routes.post("/", createPost);
routes.patch("/:id", updatePost);
router.delete("/:id", deletePost); // maybe it should be "routes"
export default routes;

-> routes.delete("/:id", deletePost);

Upvotes: 1

Related Questions