Client

const handlePublish = async () => {
    try {
      const { data } = await axios.post("/create-post", {
        title,
        content,
        categories,
      });
      // console.log("POST CREATED => ", data);
      if (data?.error) {
        toast.error(data.error);
      } else {
        toast.success("Post created successfully");
        localStorage.setItem("post-title", "");
        localStorage.setItem("post-content", "");
        router.push("/admin/posts");
      }
    } catch (err) {
      console.log(err);
      toast.error("Post create failed. Try again.");
    }
  };

Server

// route
router.post("/create-post", requireSignin, isAdmin, createPost);
// controller
exports.createPost = async (req, res) => {
  try {
    const { title, content, categories } = req.body;
    // check if title is taken
    const alreadyExist = await Post.findOne({
      slug: slugify(title.toLowerCase()),
    }).exec();
    if (alreadyExist) return res.json({ error: "Title is taken" });

    // get category ids based on category name
    let ids = [];
    for (let i = 0; i < categories.length; i++) {
      Category.findOne({ name: categories[i] }).exec((err, c) => {
        if (err) {
          console.log(err);
        }
        // console.log("c", c._id);
        ids.push(c._id);
      });
    }
    // save post
    setTimeout(async () => {
      const newPost = await new Post({
        title,
        slug: slugify(title),
        content,
        categories: ids,
        postedBy: req.user._id,
      }).save();
      return res.json(newPost);
    }, 1000);
  } catch (err) {
    console.log(err);
  }
};