Unserializable error when returning response from selecting documents from MongoDb collection. React and NodeJS
P粉818088880
P粉818088880 2023-09-16 18:20:04
0
1
473

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 withuserComplaints: 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粉818088880
P粉818088880

reply all (1)
P粉709307865

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...

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!