How to show mongoose find() method of an array inside an object

Viewed 17

I'm not sure how to explain the following, but I hope I can make myself understood. I am doing a moongose compatible query in NodeJS. That works fine, I do a search and some padding to get the expected result, but there is something I don't understand. I am doing this query through a regEx as I am searching by string parameter. When I do it this way I get something like this:

{
"affiliate": {
"0": {
...content
}
}
}

//The code of the before res using finde in affiliate

export const getAffiliateWithUserName = async (req: Request & any, res: Response, next: NextFunction) => {
  try {
    const userName: string = req.params.userName;
    const affiliate = await Affiliate.find({ "userName": { $regex: userName, $options: "i" } }).populate({
      path: "attentionSchedules",
      select: "place floor room schedules -affiliate -_id",
      populate: { path: "place", select: "name address loc" },
      match: { enabled: true }
    }).exec();
    const affiliateId = affiliate.map(id=> id._id);
    const idToString = affiliateId.toString();
    const affiliateObjectId = Types.ObjectId(idToString);
    const servicesByAff = await ServicesByAffiliate.find({
      affiliate: affiliateObjectId,
      enabled: true
    }).populate("service").populate("category").populate("subCategory").populate("place").populate({
      path: "appointmentRequiredService",
      populate: { path: "category service" }
    }).exec();
    console.log(servicesByAff);
    const groupedServices = servicesByAff.reduce((acc, serviceByAff) => {
      if (serviceByAff.service.isVirtual) {
        acc.virtual.push(serviceByAff);
      } else if (serviceByAff.service.isDomicilio) {
        acc.atHome.push(serviceByAff);
      } else {
        acc.clinic.push(serviceByAff);
      }

      return acc;
    }, { virtual: [], atHome: [], clinic: [] });

    const convertToObject = Object.assign({}, affiliate);
    res.status(200).json({
      affiliate: { ...convertToObject, groupedServices },
    });
    // res.json({ affiliate });
  } catch (error) {
    res.status(500).json({
      error: true,
      message: error.message,
    });
  }
};

It's like a nested object, but when I perform my query with findOne passing the schema id as a parameter, the result I get is:

{
"affiliate":{
...content
}
}

//The code use findOne in affiliate

export const groupedServices = async (req: Request & any, res: Response, next: NextFunction) => {
    try {
        const {affiliateId} = req.params;
        const affiliateObjectId = Types.ObjectId(affiliateId);
        const affiliate = await Affiliate.findOne(affiliateObjectId).populate({
            path: "attentionSchedules",
            select: "place floor room schedules -affiliate -_id",
            populate: {path: "place", select: "name address loc"},
            match: {enabled: true}
        }).exec();
        const servicesByAff = await ServicesByAffiliate.find({
            affiliate: affiliateObjectId,
            enabled: true
        }).populate("service").populate("category").populate("subCategory").populate("place").populate({
            path: "appointmentRequiredService",
            populate: {path: "category service"}
        }).exec();
        console.log(servicesByAff);
        const groupedServices = servicesByAff.reduce((acc, serviceByAff) => {
            if (serviceByAff.service.isVirtual) {
                acc.virtual.push(serviceByAff);
            } else if (serviceByAff.service.isDomicilio) {
                acc.atHome.push(serviceByAff);
            } else {
                acc.clinic.push(serviceByAff);
            }

            return acc;
        }, {virtual: [], atHome: [], clinic: []});

        res.status(200).json({
            affiliate: {...affiliate.toObject(), groupedServices},
        });
    } catch (e) {
        res.status(500).json({
            error: true,
            message: e.message,
        });
    }
};

And I need to display my result as the second example, what could it be? Does it have something to do with the way I display my result? Is it some moongose? Could someone give me some recommendation?

0 Answers
Related