#API call not working

3 messages · Page 1 of 1 (latest)

tender crow
#

I can't remember too much about axios, but what I do know of it is that response.json() doesn't work for this. I think it needs to be response.data which should have the user information.

If you didn't use the axios configuration to set a base url, then you will need to provide a full url path as your api variable. In other words

const api = "http://localhost:<port>/api/gomoku/generate_username"

This should be the full path to your running server on the specified port.

I also notice that in your route, you are just passing in playerId to res.json but I see no export or module.exports to show that anything is being exported from your module path "../domain/player" and it looks like the create function isn't being used. Hard to say without more code. I also do believe that playerId, if not an object, would technically be invalid json because it looks like you are attempting to send an array. Again, this is just an assumption because I can't see what is being exported from player.js and it looks as though nothing is really being created as far as usernames.

I have no idea if you've hooked up your router properly and passed it the proper way to app.use in your app.js/server.js file either. Or that the server is running.

I would also recommend that you create a post request instead and create/generate a new username. It would be, technically, more semantically correct for creating a resource. A get request should be used in the event the resource is assumed to exist. I don't know that it is proper to create a resource in a get request.

Too many unknowns to be of much help here, but this is the best advice I can give with the information provided. Hopefully it helps.

light forge
#

@main pilot As @tender crow noted above, we don't really have enough information to give a solid answer and solution to your problem, so here's some troubleshooting steps to follow instead:

  1. Is your express server running on the correct port?
  2. Is your fetch request going to the correct url? (check the network tab in the browser dev tools for this)
  3. Is the router.get callback function receiving the request? ( console.log(req) inside the callback function for this)
  4. Is the playerId variable inside the router.get callback function the correct value?
  5. Is the correct response being delivered back to the client? (check the network tab in the browser dev tools for this)

If all that is working as expected, the final step is to make sure you're processing the response correctly inside the client.

light forge
#

If you like, post the GitHub repo and we can take a closer look at it