Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST and PUT routes for React app do not work #8

Open
kstonekuan opened this issue Jan 7, 2022 · 1 comment
Open

POST and PUT routes for React app do not work #8

kstonekuan opened this issue Jan 7, 2022 · 1 comment

Comments

@kstonekuan
Copy link

kstonekuan commented Jan 7, 2022

Hi, I am unable to add or edit products as the POST and PUT routes do not work. Seems like when the "+" or "Edit" functions are used, upon saving they will send requests to http://localhost:3000/products/api/products instead of http://localhost:3000/api/products thus causing the requests to fail.

Load and delete products features both work.

Same issue faced with the authentication tutorial: https://github.com/MicrosoftDocs/mslearn-staticwebapp-authentication

@kstonekuan
Copy link
Author

kstonekuan commented Jan 7, 2022

I am able to get it working by changing the route that is requested for add and edit in react-app/src/store/product.api.js as such:

import axios from 'axios';
import { parseItem, parseList } from './action-utils';
import API from './config';

const captains = console;

export const deleteProductApi = async (product) => {
  const response = await axios.delete(`${API}/products/${product.id}`);
  return parseItem(response, 200);
};

export const updateProductApi = async (product) => {
  captains.log(product.id);
  const response = await axios.put(`../${API}/products/${product.id}`, product); // Here
  return parseItem(response, 200);
};

export const addProductApi = async (product) => {
  const response = await axios.post(`../${API}/products`, product); // Here
  return parseItem(response, 201);
};

export const loadProductsApi = async () => {
  const response = await axios.get(`${API}/products`);
  return parseList(response, 200);
};

EDIT: You can also change the config to have a / in front like /api so it is an absolute path instead of relative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant