0

I am using npm concurrently to run front end and backend at the same time, but still not connecting to the backend correctly. The path is generating correctly and works in postman, but not when I call it from the site. I've run it through the debugger, and narrowed it down. I am sure it has something to do with how I have my proxy set up, but I am not sure exactly were I went wrong HELP!

Front end package.json

      "proxy": "http://myIPaddress:4001",
      "secure": false

action in redux:

import {LOGIN} from './types'

const baseline = `http://myIPaddress/`

export const login = (username, password) => async dispatch => {
  console.log('tell me the username', username.value, " and your password? ", password.value)
  let path = `postLogin/`
  let params = `${username.value}/${password.value}`

  console.log('the path : ', `${baseline}${path}${params}`)

    try{
      // axios.default.header.get['Content-Type'] = ''
        let res = await fetch(`${baseline}${path}${params}`,{
          method: 'GET',
          headers: {
              'Content-type': 'application/json'
            }
        }
        )
        .then(res => res.json())
        .then(
        dispatch( {
            type: LOGIN,
            payload: res.json()
        })
      )
    }
    catch(e){
        console.log(`can't find that user ${username.value}`)
    }

}

If there is a section I did not include you all need in order to help me solve this, please let me know.

THANKS!

  • What is the error and why would you fetch from `http://myIPaddress/` if you set up a proxy in your package.json? You should fetch from `/api/entity/...` – HMR Nov 29 '20 at 07:49
  • Have you checked if you're getting any errors in the console log .. of so please post the error – Mazen Sharkawy Nov 29 '20 at 07:51
  • @HMR I get back my catch, `can't find the user `, but the call is valid in postman. – N. Kendrick Nov 29 '20 at 14:49
  • @MazenSharkawy no error come in the console. It just get back my catch response though I know the call is valid, I've change it to every use in my database just to make sure I wasn't doing something wrong with that one entry, but it always catchs. – N. Kendrick Nov 29 '20 at 14:55
  • I need you to log the error in the catch block, something like this: catch(error){ console.log({error}) } – Mazen Sharkawy Nov 29 '20 at 16:08
  • @N.Kendrick The devtools also have a network tab, you should find the failing request there but still don't understand why you provide the full url in the fetch while also setting proxy in the package.json. – HMR Nov 29 '20 at 17:37
  • @MazenSharkawy since have: what is the error? TypeError: Failed to fetch {stack: 'TypeError: Failed to fetch', message: 'Failed to fetch'} – N. Kendrick Nov 29 '20 at 20:56
  • there is an error in the last .then statement, like this: .then( json => dispatch( { type: LOGIN, payload: json }) however, there is probably another error – Mazen Sharkawy Nov 30 '20 at 10:22

0 Answers0