简体   繁体   English

React useState 设置来自数据库的响应,但是当我尝试访问数据 [0].listing_reviews 时,它返回一个未定义的错误

[英]React useState sets response from database but when I try to access the data[0].listing_reviews it returns an undefined error

I need some help understanding why my data can be accessed at index[0] but as soon as I try to access a key on it, it returns an undefined error.我需要一些帮助来理解为什么可以在索引 [0] 访问我的数据,但是一旦我尝试访问它上面的键,它就会返回一个未定义的错误。 The initial state of my data is an empty array.我数据的初始 state 是一个空数组。 I've confirmed that what I get back from the response is indeed an array of length 1 with an object inside of it.我已经确认我从响应中得到的确实是一个长度为 1 的数组,其中包含一个 object。

App.jsx应用程序.jsx

import React, { useState, useEffect } from 'react';
import Reviews from './Reviews.jsx';
import axios from 'axios';

const ReviewsApp = () => {
  const [listingData, setlistingData] = useState([]);

  useEffect(() => {
    let id = Math.floor(Math.random() * 100) + 1 // Random Test ids
    axios
      .get(`listings/${id}/reviews`)
      .then(res => setlistingData(res.data))
      .catch(err => console.log(err));
  }, []);

  return (
    <div>
      <Reviews listingData={listingData}/>
    </div>
  );
};

export default ReviewsApp;

Review.jsx评论.jsx

import React, { useState } from 'react';

const Reviews = ({ listingData }) => {

  return (
    <div>
      {console.log(listingData[0].listing_reviews)}
    </div>
  )
}

export default Reviews;

So right now, if I console.log(listingData[0]) it works no problem and returns an object with the following structure:所以现在,如果我使用 console.log(listingData[0]) 它可以正常工作并返回具有以下结构的 object:

listing_id: 0,

listing_rating: 1.5,

listing_reviews: [] <---Full of objects

As soon I try to access one of those keys it fails by giving me an undefined error.一旦我尝试访问其中一个键,它就会通过给我一个未定义的错误来失败。 Please let me know if you would like some more info as well.如果您还需要更多信息,请告诉我。 Thank you.谢谢你。

The problem is that you're rendering the result of console.log in your Reviews.jsx file.问题是您在 Reviews.jsx 文件中呈现console.log的结果。 Anything you put in curly braces {} in JSX, React will render.你在 JSX 中放在大括号{}中的任何内容,React 都会渲染。 So if you call a function, React will try to render the return value of that function. React throws an error if you try to render undefined .因此,如果您调用 function,React 将尝试呈现该 function 的返回值。如果您尝试呈现undefined ,React 会抛出错误。 And since console.log always returns undefined , you're always going to get an error.由于console.log总是返回undefined ,你总是会得到一个错误。 Even if you eventually have data in your listingData prop, since you're still trying to render the return of console.log and it still returns undefined , you'll get an error.即使您最终在listingData道具中有数据,因为您仍在尝试呈现console.log的返回并且它仍然返回undefined ,您将收到错误。

So, just get rid of the console.log .所以,只需摆脱console.log Also, you will probably want to check that you actually have data in your listingData prop.此外,您可能想要检查您的listingData道具中是否确实有数据。 Otherwise you'll get an error before your data actually loads.否则,您会在数据实际加载之前收到错误消息。

So, something like this for your Reviews.jsx file:所以,你的Reviews.jsx文件是这样的:

import React, { useState } from 'react';

const Reviews = ({ listingData }) => {
  // you can still add a debug line up here. that won't be an issue
  console.log(listingData);

  const reviews = listingData.length ? listingData[0].listing_reviews : null;

  return (
    <div>
      {reviews}
    </div>
  )
}

export default Reviews;

暂无
暂无

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

相关问题 当我尝试访问特定值时,为什么我的JSON数组响应返回未定义 - Why is my JSON array response returns undefined when I try to access specific value 为什么我尝试访问异步函数时会返回一个未定义的? - Why my async function returns an undefined when i try to access it? 当我尝试获取它的特定元素时,React 数组返回未定义 - React array returns undefined when i try to get specific element of it 当我尝试检索JSON数据时,Cheerio返回未定义 - Cheerio returns undefined when I try to retrieve JSON data 来自 API 的响应返回数据,状态未定义 - React.JS - Response from API returns data, state is undefined - React.JS 当我尝试访问它时无法访问反应道具数据。 获取 TypeError 错误 - Can not access react props data when I try to access it. Getting TypeError error 我从 Api 获取数据,但是当我尝试使用 useState 设置值时,它给了我一个空的 object - I get data from Api but when I try to set the value with useState it give me an empty object 如何在下一个 useState 中访问一个 useState 的数据 - How can i access the data from one useState in the next useState 尝试在 React 上使用 keydown 读取和更改 useState 的值时出现未定义错误 - Undefined error when trying to read and change value from useState using keydown on React 当我尝试从Ajax进行POST时PHP未定义的索引数据 - PHP Undefined index data when I try to POST from Ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM