1

I want to create a complex nested document which can store values like this

category: {
  "fish": ["Sardines", "Goldfish"],
  "dogs": ["German Shepherd", "Dobberman"]
}

Here's what I tried

export const CategorySchema = new mongoose.Schema(
  {
    category: {
      type: Map,
      of: [String],
    },
  },
  { timestamps: true }
);

I passed data like this (from console) this is how the passed data looks like Nothing is being created in the database. Also no error.

export default async (req, res) => {
  const { method } = req;
  switch (method) {
    case "GET":
      try {
        const categories = await Category.find({});
        res.json({ success: true, data: categories });
      } catch (error) {
        res.json({ success: false });
      }
      break;
    case "POST":
      try {
        let data = req.body;
        data = JSON.parse(data);
        const category = new Category(data);
        const doc = await category.save();
        console.log("Doc from Categories API", doc);
        res.json({ success: true, data: doc });
      } catch (error) {
        res.json({ success: false });
      }
      break;
    default:
      res.status(400).json({ success: false });
      break;
  }
};

Can anyone advise me, please?

onMyWay
  • 117
  • 1
  • 1
  • 7

0 Answers0