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

174
Views
Se arroja un error que afirma que un método creado en una clase axios no es una función

En mi aplicación de reacción, estoy definiendo una clase Axios con un montón de métodos, pero los métodos no se reconocen como funciones y arrojan un error. Mostrar es más fácil que explicar así que... tengo 3 archivos involucrados...

http-common.js tiene esto:

 import axios from 'axios'; export default axios.create({ baseURL: "http://localhost:5000/api/v1/tours", headers: { "Content-type": "application/json" } });

tours.js tiene esto:

 import http from "../http-common"; class ToursDataService { getAll(page = 0) { return http.get(`?page=${page}`); } } export default ToursDataService

tours-list.js tiene esto... que llama a la función "getAll" en retrieveTours.

 import React, { useState, useEffect } from "react"; import ToursDataService from "../services/tours"; const ToursList = props => { const [tours, setTours] = useState([]); useEffect(() => { retrieveTours(); }, []); const retrieveTours = () => { ToursDataService.getAll() .then(response => { setTours(response.data.tours) }) .catch( e => { console.log(e); }); }

La consola afirma que getAll no es una función. ¿Por qué? ¿Alguien puede explicar?

 scheduler.development.js:173 Uncaught TypeError: _services_tours__WEBPACK_IMPORTED_MODULE_1__.default.getAll is not a function at retrieveTours (tour-list.js:12:1)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

getAll() no es un método estático, por lo que deberá crear una instancia de ToursDataService ...

 const svc = new ToursDataService(); // create an instance // ... svc.getAll() // call the method on the instance .then(...)

o hacer que el método sea estático

 class ToursDataService { static getAll(page = 0) { return http.get("", { params: { page } }); } }

Alternativamente, no use clases en absoluto ya que no parece estar encapsulando nada. También podría exportar la función getAll por sí sola

 // tours.js export const getAll = (page = 0) => http.get("", { params: { page } });

y

 import { getAll } from "../services/tours";
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!