WDI-SEA / project-4-issues

Open an issue to receive help on project 4 issues
0 stars 0 forks source link

Fetched data from database not passing back to client #6

Closed harrdev closed 2 years ago

harrdev commented 2 years ago

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

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))
    }
  }

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

timmshinbone commented 2 years ago

what does the console log show then if it's not the data? Is the console log even hitting?

harrdev commented 2 years ago

Screen Shot 2022-01-10 at 8 17 57 AM

The console log on the client is hitting

harrdev commented 2 years ago

Resolved. Timm is a god.