Search code examples
javascriptreactjsmongoosereact-reduxredux-toolkit

Some data returns undefined using RTK Query


This is my first time using Redux, what happens is that I am trying to show data in a Material UI Select, and when I show the user name it works perfectly, I did the same for the partner's data, but they arrived as undefined.

In the server index, I have the following code: app.use("/client", clientRoutes);

In the Routes I have this:

import express from "express";
import { getAllUsers, getPartner } from "../controllers/client.js";

const router = express.Router();

router.get("/users", getAllUsers);
router.get("/partners", getPartner);

export default router;

In the controller this:

import User from "../models/User.js";
import Partner from "../models/Partner.js";

export const getAllUsers = async (req, res) => {
  try {
    const users = await User.find();
    res.status(200).json(users);
  } catch (error) {
    res.status(404).json({ message: error.message });
  }
};

export const getPartner = async (req, res) => {
  try {
    const partners = await Partner.find();
    res.status(200).json(partners);
  } catch (error) {
    res.status(404).json({ message: error.message });
  }
};

In the model this:

import mongoose from "mongoose";

const PartnerSchema = new mongoose.Schema(
  {
    partner_name: {
      type: String,
      required: true,
      min: 2,
      max: 100,
    },
    partner_phone: {
      type: String,
      min: 2,
      max: 100,
    },
    partner_user: {
      type: String,
      required: true,
      min: 2,
      max: 100,
    },
    partner_password: {
      type: String,
      required: true,
      min: 2,
      max: 100,
    },
  },
  { timestamps: true }
);

const Partner = mongoose.model("Partner", PartnerSchema);
export default Partner;

In the api this:

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const api = createApi({
  reducerPath: "adminApi",
  baseQuery: fetchBaseQuery({ baseUrl: process.env.REACT_APP_BASE_URL }),
  tagTypes: ["User", "Users", "Partners"],
  endpoints: (build) => ({
    getUser: build.query({
      query: (id) => `general/user/${id}`,
      providesTags: ["User"],
    }),
    getAllUsers: build.query({
      query: () => "client/users",
      providesTags: ["Users"],
    }),
    getPartner: build.query({
      query: () => "client/partners",
      providesTags: ["Partners"],
    }),
  }),
});

export const { useGetUserQuery, useGetAllUsersQuery, useGetPartnerQuery } = api;

and in my component the following imports::

import { useGetAllUsersQuery } from "state/api";
import { useGetPartnerQuery } from "state/api";

and then this code:

  const { data } = useGetAllUsersQuery();
  const { data2 } = useGetPartnerQuery();
  console.log("usuarios", data);
  console.log("partners", data2);

That's where I realize that the data arrives undefined. enter image description here

Thank you very much to those who can help me!


Solution

  • At the line of

    const { data } = useGetAllUsersQuery();
    

    You are properly destructuring the data property, but the issue is that on the following line of

    const { data2 } = useGetPartnerQuery()
    

    You are destructuring non-existant data2 property which has value undefined, instead you should be destructuring data property and rename it to data2 using colon

    const { data: data2 } = useGetPartnerQuery()