【问题标题】:What I'm missing in npm apisauce我在 npm apisauce 中缺少的东西
【发布时间】:2021-10-21 15:55:53
【问题描述】:

我正在学习本机反应,因此我正在使用名为“apisauce”版本^2.1.2的api包。

当后端被邮递员调用时,我收到一个数据数组,当它被前端应用程序调用时,我收到“Network_Error”

我的问题是我做错了什么?

client.js:

import { create } from "apisauce";

const apiClient = create({
  baseURL: "http://127.0.0.1:9000/api",
});

export default apiClient;

listings.js:

import client from "./client";

const endpoint = "/listings";
const getListings = () => client.get(endpoint);

export default { getListings };

App.js

import React, { useEffect } from "react";
import listingsApi from "./app/api/listings";

function App() {
  const loadData = async () => {
    const response = await listingsApi.getListings();
    console.log(response);
  };

  useEffect(() => {
    loadData();
  }, []);

  return (
    <Screen>
    </Screen>
   );
}

export default App;

这是我执行 console.log 时的错误:

【问题讨论】:

    标签: javascript apisauce


    【解决方案1】:

    我找到了一个解决方案,在该解决方案中,我必须将 baseURL 设置为本地 IP 地址,而必须将 baseURL 设置为我的计算机的 IP 地址。

    【讨论】:

    • 您还可以使用 android SDK 工具中的“adb reverse”命令将您的设备本地端口指向计算机本地端口。这样,您甚至可以使用设备的 3G 连接进行测试
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多