title: How to make post category to be seen on the Database?
body: <p>I'm unable to see my category in mongodb database.</p>
  <p>I want to create a post along with a category, but unfortunately, my category array is Empty.</p>
  <p>and the error on my console says... TypeError: Cannot read properties of null (reading '_id') on line 24 which is <strong>ids.push(data._id)</strong></p>
  <pre><code>exports.createPost = async (req, res) =&gt; {
    const { file, body } = req;
  
    const { title, content, categories } = body;
    console.log(categories);
  
    // check if title is taken
    const findPost = await Post.findOne({ slug: slugify(title.toLowerCase()) });
    if (findPost) return sendError(res, &quot;This Post Title already exists!&quot;);
  
    // get category ids based on category name
    let ids = [];
    for (let i = 0; i &lt; categories.length; i++) {
      await Category.findOne({
        name: categories[i],
      }).exec((error, data) =&gt; {
        if (error) return sendError(res, &quot;Error occurred in Category&quot;);
        ids.push(data._id);
      });
    }
  
    const newPost = await new Post({
      ...body,
      slug: slugify(title),
      categories: ids,
      postedBy: req.user._id,
    });
  </code></pre>
  <p>my <strong>Category Controller</strong></p>
  <pre><code>exports.createCategory = expressAsyncHandler(async (req, res) =&gt; {
    const { name } = req.body;
    // Check if the Category exists
    const findCategory = await Category.findOne({ name });
    if (findCategory) return sendError(res, &quot;This category already exists!&quot;);
    const category = await new Category({
      name,
      slug: slugify(name),
    }).save();
    res.json(category);
  });
  </code></pre>