جاوااسکریپت - TypeError: axios.get یک تابع نیست؟
مطمئن نیستم چرا این خطا را دریافت میکنم:
TypeError: axios.get is not a function
4 |
5 | export const getTotalPayout = async (userId: string) => {
> 6 | const response = await axios.get(`${endpoint}get-total-payout`, { params: userId });
7 | return response.data;
8 | };
9 |
خدمات من:
import * as axios from 'axios';
const endpoint = '/api/pool/';
export const getTotalPayout = async (userId: string) => {
const response = await axios.get(`${endpoint}get-total-payout`, { params: userId });
return response.data;
};
آزمایش جست من:
// import mockAxios from 'axios';
import { getTotalPayout } from './LiquidityPool';
const userId = 'foo';
describe('Pool API', () => {
it('getTotalPayout is called and returns the total_payout for the user', async () => {
// mockAxios.get.mockImplementationOnce(() => {
// Promise.resolve({
// data: {
// total_payout: 100.21,
// },
// });
// });
const response = await getTotalPayout(userId);
console.log('response', response);
});
});
در فایل src/__mocks__/axios.js من این را دارم:
// tslint:disable-next-line:no-empty
const mockNoop = () => new Promise(() => {});
export default {
get: jest.fn(() => Promise.resolve({ data: { total_payout: 100.21 }})),
default: mockNoop,
post: mockNoop,
put: mockNoop,
delete: mockNoop,
patch: mockNoop
};
پاسخ شماره ۱
لطفاً نگاه کنید به: MDN
همانطور که در آنجا ذکر شد، شما به یک مقدار برای جمعآوری export پیشفرض و بقیه به عنوان X نیاز دارید. در این مورد میتوانید:
import axios, * as others from 'axios';
X اینجا others است.
به جای
import * as axios from 'axios';
فرض: ... from 'axios' به mock جست شما اشاره دارد.
پاسخ شماره ۲
به جای import { Axios } from "axios"; از import Axios from "axios"; استفاده کنید
