Keep failing passing arrays to axios.get request to retrieve data from Mongoose

Viewed 125

I have .get request on mongoose as below

  static async getUserInitialPlace(favCategories, userCoordinates) {
    try {
      const places = await Place.aggregate([
        {
          $geoNear: {
            near: { type: 'Point', coordinates: userCoordinates },
            distanceField: 'calcDistance',
            maxDistance: 2500000,
            spherical: true,
            query: {
              category: {
                $in: [favCategories],
              },
            },
          },
        },
      ]);
      return places;
    } catch (err) {
      return console.log(err);
    }
  }

And, my redux saga looked like this

function* getUserInitialPlace({ favCategories, userCoordinates }) {
  const req = {
    params: {
      coordinate: userCoordinates,
      sports: favCagetories,
    },
  };
  try {
    const response = yield axios.get(
      `${config.baseUrl}/places/getuserinitialplace`,
      req
    );

I and passing below data to my redux sage

  getUserInitialPlace([user.categories], [user.location_coordinates.lng, user.location_coordinates.lat]);

{type: "REQUEST_GET_USER_INITIAL_PLACE", favCategories: Array(1), userCoordinates: Array(2)}
favCategories: Array(1)
0: "5e5150c6c52a3904b74d6ff7"
type: "REQUEST_GET_USER_INITIAL_PLACE"
userCoordinates: Array(2)
0: -86.158068
1: 39.768403

However, my node is receiving HTTP with

"GET /places/getuserinitialplace?coordinate[]=126.7052062&coordinate[]=37.4562557&favCategories[]=[%225e5150bcc52a3904b74d6ff6%22,%225e5150d6c52a3904b74d6ff8%22,%225e5150c6c52a3904b74d6ff7%22,%225e5150ffc52a3904b74d6ffc%22]

how can I send the params inside of the [] part or correctly format the request link for mongoose to proceed the get request?

1 Answers

I think you can adjust your params object as follows to achieve your desired result:

const req = {
  params: {
    coordinate: `[${userCoordinates.join(",")}]`,
    sports: `[${favCagetories.join(",")}]`,
  },
};
Related