Closed harrdev closed 2 years ago
MERN
Fetching data from seeded database working on server, but not passing back to client
SERVER CODE
router.get('/', (req, res, next)=> { // console.log("Request from server before db call") Asana.find() .then((asanas) => { //console.log("Server-side fetch req received", asanas) console.log("This is the mapped asanas: ", asanas.map((asana => asana.toObject()))) return asanas.map((asana) => { asana.toObject() }) }) .then((asanas) => res.status(200).json({ asanas: asanas })) .catch(next) })
CLIENT CODE
const getAsanas = () => { if (user != null) { fetch(apiUrl + '/', { // Requires user to be signed in headers: { 'Authorization': 'Bearer ' + user.token } }) .then(res => { console.log('These are the seeded asanas', res) // return asanas.json() }) .catch(error => console.log('Failed to fetch', error)) } }
No error message. Client console.log does not show an object of the data
Either I'm not accessing data correctly in the client, or not passing it correctly in the server
Using .json and .toObject
what does the console log show then if it's not the data? Is the console log even hitting?
The console log on the client is hitting
Resolved. Timm is a god.
What stack are you using?
(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)
MERN
What's the problem you're trying to solve?
Fetching data from seeded database working on server, but not passing back to client
Post any code you think might be relevant (one fenced block per file)
SERVER CODE
CLIENT CODE
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
No error message. Client console.log does not show an object of the data
What is your best guess as to the source of the problem?
Either I'm not accessing data correctly in the client, or not passing it correctly in the server
What things have you already tried to solve the problem?
Using .json and .toObject