可以用React-Query提取数据

发布于 2025-01-25 20:54:41 字数 257 浏览 1 评论 0原文

在我的客户端(React JS)上,我想从后端获取数据。但这不起作用。数据的输出不明智。 代码片段,我将Express JS和MongoDB用作数据库。

  const url = `http://localhost:5000/items/${id}`;
  const { data } = useQuery("data", () => axios(url));
  console.log("data", data);

后端的

On my client-side(React js), I want to fetch data from the backend. But it's not working. The output of data is undefiend. Code Snippets

  const url = `http://localhost:5000/items/${id}`;
  const { data } = useQuery("data", () => axios(url));
  console.log("data", data);

In the backend, I am using Express js and MongoDB as databases.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2025-02-01 20:54:41

尝试创建用于获取的函数,例如:

const fetchData = async (id) => {
  try {
    const URL = `http://localhost:5000/items/${id}`;
    const response = await axios.get(URL);
    return response;
  } catch(error) {
    console.log(error);
  }
}

然后:

const { data } = useQuery("data", () => fetchData(id));

您必须将ID提供给fetchdata

Try creating a function for fetching, for example:

const fetchData = async (id) => {
  try {
    const URL = `http://localhost:5000/items/${id}`;
    const response = await axios.get(URL);
    return response;
  } catch(error) {
    console.log(error);
  }
}

Then:

const { data } = useQuery("data", () => fetchData(id));

You have to provide the id to fetchData

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文