【问题标题】:Uncaught TypeError: Cannot convert undefined or null to object未捕获的类型错误:无法将未定义或 null 转换为对象
【发布时间】:2022-12-17 08:59:51
【问题描述】:

在此行中出现错误,因为无法将 undefined 或 null 转换为 abject 我还突出显示了显示错误的行

import React from 'react';
import { useParams } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { DetailsHeader, Error, Loader, RelatedSongs } from '../components';

import { useGetArtistDetailsQuery } from '../redux/sevices/shazamCore';

const ArtistDetails = () => {
  const { id: artistId } = useParams();
  const { activeSong, isPlaying } = useSelector((state) => state.player);
  const { data: artistData, isFetching: isFetchingArtistDetails, error } = useGetArtistDetailsQuery(artistId);

  if (isFetchingArtistDetails) return <Loader title="Loading artist details..." />;

  if (error) return <Error />;

  return (
    <div className="flex flex-col">
      <DetailsHeader
        artistId={artistId}
        artistData={artistData}
      />

      <RelatedSongs
`***        data={Object.values(artistData?.songs)}    //// error in this line ***`
        artistId={artistId}
        isPlaying={isPlaying}
        activeSong={activeSong}
      />
    </div>
  );
};

export default ArtistDetails;

我不确定这是什么错误
data={Object.values(artistData?.songs)}

【问题讨论】:

    标签: reactjs jsx rapidapi


    【解决方案1】:

    如果歌曲为空,则定义默认值

    Object.values(artistData?.songs || {})
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2020-09-27
      相关资源
      最近更新 更多