search

Home  >  Q&A  >  body text

Unserializable error when returning response from selecting documents from MongoDb collection. React and NodeJS

My goal is just to return complaints that belong to this user.

I have two collections in MongoDB. Users and complaints. One user has one complaint and another user has two complaints.

I'm using Redux to create an initial state with userComplaints: null.

For its reducer, I write: onlyUserComplaints: (state, { Payload }) => { state.userComplaints = Payload; localStorage.setItem('userComplaints', JSON.stringify(payload)); } ,

This is the relevant code for this operation, `const config = { title:{ 'content-type': 'application/json', }, };

const data = await axios.get(
  `http://localhost:5000/api/complaint/gettingusercomplaints/${userId}`,
  config
);

dispatch(onlyUserComplaints(data))`

My route can be found in Postman as follows: `const getAllUserComplaints = asyncHandler(async (req, res) => { try { const userId = req.params.id;

const userComplaints = await Complaint.find({ userId });

res.status(201).json(userComplaints);

} catch(error){ throw new AppError(Something went wrong, 404); } });`

For this route, I have a similar Get All Complaints which successfully returns three complaints. The relevant code is, constcomplaint=awaitComplaint.find({});

I tried replacing find with findById but it failed in Postman when only wanting to get complaints for a given user.

Look at the actual error:

So, this is what I don't understand. The data populates just fine in Postman. However, when I console.log(userComplaints) the data returned, its type is not an object, like when I query find({})... when I ask for all complaints.

More information here:

Any ideas?

But the postman said it was no problem.

P粉818088880P粉818088880441 days ago594

reply all(1)I'll reply

  • P粉709307865

    P粉7093078652023-09-17 12:03:36

    Lanxion pointed out on Discord that I wasn't deconstructing the data in the response, so I was getting other stuff in the headers, like options.

    const {data} = axios...

    reply
    0
  • Cancelreply