'How to save a embedded nested array in mongoose using post rest API in node js?

I have an embedded mongoose schema in which I have a schema field as Orgdata which has a parent array and inside it has an object which has two child array hospitalList and clinicList.

Orgdata: [{ hospitalList: [String], clinicList: [String] }],

I have a post route /addorg where I want to save this Orgdata ...How to do it? Anyone please help.

 router.post("/addorg", (req, res, next) => {
  const file = req.files.image;
  cloudinary.uploader
    .upload(file.tempFilePath, (err, result) => {
      console.log(result);

      data = new Org({
        _id: new mongoose.Types.ObjectId(),
        OrgName: req.body.OrgName,
        image: result.url,
         
        Status: req.body.Status,
        Availability: req.body.Availability,
        ServiceTiming: req.body.ServiceTiming,
        OrgType: req.body.OrgType,
        OrgAddress: req.body.OrgAddress,
        OrgLocation: req.body.OrgLocation,
        OrgCity: req.body.OrgCity,
        State: req.body.State,
        Country: req.body.Country,
        OrgDescription: req.body.OrgDescription,
        ContactName: req.body.ContactName,
        ContactPhoneNumber: req.body.ContactPhoneNumber,
        ContactEmailId: req.body.ContactEmailId,
      });

      data
        .save()

        .then((result) => {
          res.status(201).json({
            message: "Organization added successfully",
            createdOrg: result,
          });
        });
    })
    .catch((err) => {
      res.status(500).json({ error: err });
    });
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source