简体   繁体   English

我正在尝试显示来自外部 api 的数据,如果有人可以帮助我解决代码中的问题

[英]i am trying to display data from an external api, if someone can please help me to fix the issue in my code

this is the error message i am getting TypeError: undefined is not an object (evaluating 'drinkList.drinks[0]')这是我收到的错误消息 TypeError: undefined is not an object (evaluating 'drinkList.drinks[0]')

can you help me please fix the error and give me some solution so that i can use the app to fetch data from the external api?你能帮我修复错误并给我一些解决方案,以便我可以使用该应用程序从外部api获取数据吗?

this is my drink.js code:这是我的 Drink.js 代码:

import React, { useEffect, useState } from "react";
import axios from "axios";
import Drinks from "./Drinks";

function Home() {
  const [drinkName, setDrinkName]= useState([]);
  const [drinkList, setDrinkList] = useState([]);
  const drinksURL = `https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${drinkName}`;

  const handleChangeDrink= e => {
    setDrinkName(e.target.value);
  }

  const getDrink = () => {
    axios
      .get(drinksURL)
      .then(function (response) {
        setDrinkList(response.data);
        console.log(drinksURL);
      })
      .catch(function (error) {
        console.warn(error);
      });
  };

  return (
    <main className="App">
      <section className="drinks-section">
        <input
          type="text"
          placeholder="Name of drink (e.g. margarita)"
          onChange={handleChangeDrink}
        />
        <button onClick={getDrink}>Get a Drink Recipe</button>
        <Drinks drinkList={drinkList} />
      </section>
    </main>
  );
}

export default Home;

this is my Drink.js code:这是我的 Drink.js 代码:

import React from "react";

function Drinks({ drinkList }) {
  if (!drinkList) return <></>;
  return (
    <section className="drinkCard">
      <h1>{drinkList.drinks[0].strDrink}</h1>
    </section>
  );
}

export default Drinks;

Couple of problems here...这里有几个问题......

  1. drinkName is initialised as an array but it appears to be expecting a string drinkName被初始化为一个数组,但它似乎需要一个字符串
  2. drinksUrl is never updated drinksUrl永远不会更新
  3. An empty array is still truthy空数组仍然是真的

Some easy fixes一些简单的修复

const [drinkName, setDrinkName]= useState(null) // initialise as null
const [drinkList, setDrinkList] = useState([])

// don't include the "s" parameter here
const drinksURL = "https://www.thecocktaildb.com/api/json/v1/1/search.php"

const getDrink = () => {
  // pass drinkName as a param
  axios.get(drinksURL, {
    params: { s: drinkName }
  }).then(({ data }) => {
    setDrinkList(response.data)
  }).catch(console.warn)
}

and in Drink.jsDrink.js

function Drinks({ drinkList }) {
  // check the array length
  return drinkList.length && (
    <section className="drinkCard">
      <h1>{drinkList.drinks[0].strDrink}</h1>
    </section>
  )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以帮我解决这个问题吗? - Can someone help me fix this? 我在我的 ejs 文件中遇到了一些错误,有人可以帮助我吗? - I am facing the some error in my ejs file can someone please assist me? 我正在尝试使用yeoman和骨干创建示例应用程序,但显示一些错误有人可以帮助我吗? - I am trying to create sample app using yeoman and backbone but display some error can anyone help me? 我对 discord.js rpc 有错误? 有人可以帮帮我吗? - I have an error for discord.js rpc! Can someone please help me? 当我编写 npm start 并收到此错误时,我的反应应用程序没有启动,任何人都可以帮助我,我是新来的反应。 我正在使用 Acode(Editor) 和 termux - My react app does not start when I write npm start and get this error can anyone please help me I am new to react. I am using Acode(Editor) and termux 我在终端中有一个错误节点,你能帮我解决它吗 - I have an error Node in Terminal, can you help me please to fix it 我正在尝试通过运行“yarn”在 react web 应用程序中安装依赖项,但出现此错误,有人可以帮助我解决该问题 - Am trying to install dependencies in a react web app by running "yarn" but i get this error ,could someone help me on how to solve it 有人可以向我解释我在哪里可以从Google Cloud获得属性以连接到sql数据库吗? - Can someone please explain to me where do I get the properties to connect to an sql database from google cloud? 我的 Discord.JS 代码有一个奇怪的错误,你能帮我解决这个问题吗? - My code for Discord.JS is having a strange error, can you please help me solve this? 在我的 ReactJS App 的新任务中实现 ChartJS 的问题,请让我知道我错在哪里 - Issue in implementing ChartJS in my new assignment of ReactJS App, please let me know where I am wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM