Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Cómo pasar la identificación correctamente al punto final de la API Rest desde React

Estoy tratando de obtener datos a través del punto final del punto final de Django Rest Framework:

 /api/v1/categories/nested/{id}/

El problema es que cuando estoy solicitando una identificación, el servidor Django muestra este error:

 ValueError: Field 'id' expected a number but got 'undefined'. [07/Feb/2022 15:53:01] "GET /api/v1/categories/nested/undefined/ HTTP/1.1" 500 162581

Como esto sugiere que no puedo pasar la identificación correctamente, necesito un poco de ayuda para arreglar que estoy usando acciones> reductor> almacenar> enfoque de componente usando reaccionar redux action.js

 export const listCategoryDetails = (id) => async (dispatch) => { try { dispatch({ type: CATEGORY_DETAIL_REQUEST }); const { data } = await axios.get(`/api/v1/categories/nested/${id}`); // Purpose to show nested brands[] dispatch({ type: CATEGORY_DETAIL_SUCCESS, payload: data, }); } catch (error) { dispatch({ type: CATEGORY_DETAIL_FAIL, payload: error.response && error.response.data.detail ? error.response.data.detail : error.message, }); } };

reductor.js

 export const categoryDetailsReducer = ( state = { category: { } }, action ) => { switch (action.type) { case CATEGORY_DETAIL_REQUEST: return { loading: true, ...state }; case CATEGORY_DETAIL_SUCCESS: return { loading: false, category: action.payload }; case CATEGORY_DETAIL_FAIL: return { loading: false, error: action.payload }; default: return state; } };

tienda.js

 const reducer = combineReducers({ categoryDetail: categoryDetailsReducer, });

componente

 function CategoryDetail({ match, history }) { // const { id } = useParams(); // console.log(id); const dispatch = useDispatch(); const categoryList = useSelector((state) => state.categoryList); const { loading, error, categories , page, pages} = categoryList; useEffect(() => { dispatch(listCategoryDetails()); }, [dispatch, match]); return <div> {categories.map(category => ( <Col key={category.id} sm={12} md={8} lg={4} xl={3} > <h1><strong>{category.title}</strong></h1>))} </div>; } export default CategoryDetail;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const id = ... y páselo a la función de dispatch(listCategoryDetails(id))

about 4 years ago · Juan Pablo Isaza Report

0

Antes

 // const { id } = useParams(); // console.log(id); const dispatch = useDispatch(); const categoryList = useSelector((state) => state.categoryList); const { loading, error, categories , page, pages} = categoryList; useEffect(() => { dispatch(listCategoryDetails()); }, [dispatch, match]);

Después

 const { id } = useParams(); console.log(id); const dispatch = useDispatch(); const categoryList = useSelector((state) => state.categoryList); const { loading, error, categories , page, pages} = categoryList; useEffect(() => { dispatch(listCategoryDetails(id)); }, [dispatch, match]);

Dentro de UseEffect, no está pasando la variable de id , por lo que dice que la identificación no está definida

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!