即使get在起作用,也没有显示数据

发布于 2025-02-10 10:11:27 字数 3808 浏览 2 评论 0原文

我目前正在尝试获取在我的React应用程序上显示的数据。

我的查询设置为:

interface ResultWrapper<T> {
result: T;
}
export interface FinancialData {
handleViewData: () => void;
symbol: string;
name: string;
stockExchange: string;
exchangeShortName: string;
currency: string;
}

export const useGetFinancialDataKey = 'get-financial-data';

const useGetFinancialData = (
  options: UseQueryOptions<FinancialData[], string> = {}
): UseQueryResult<FinancialData[], string> =>
   useQuery<FinancialData[], string>(
   useGetFinancialDataKey,
     async () =>
     (await axiosInstance.get<ResultWrapper<FinancialData[]>>. 
     ('https://financialmodelingprep.com/api/v3/search? 
     query=AA&limit=10&exchange=NASDAQ&apikey=ENTERAPIKEY'
     )).data.result, options
     );


export default useGetFinancialData;

我已经检查了“网络”选项卡,并且回复回复:

但是,每当我转到主页时,什么也没有显示,每当我在finance.tsx文件中调用查询时,我

const {data: financialData} = useGetFinancialData();

在finance.tsx中使用完整文件时就会得到一个不确定的内容。tsx看起来像这样

    const Finance: React.FC = () => {
    const {data: financialData} = useGetFinancialData();
    const [searched, setSearched] = useState<string>('');
    const [dataToView, setDataToView] = useState<FinancialData>();
    const [isViewDialogOpen, setIsViewDialogOpen] = useState(false);
  
    const [page, setPage] = useState(0);
    const PER_PAGE = 10;
  
    const handleViewData = useCallback(
      (data: FinancialData) => {
        setDataToView(data)
        setIsViewDialogOpen(true);
      },
      [setDataToView, setIsViewDialogOpen]
    );
    const filteredRows = useMemo(()=>{
      const query = searched.toLowerCase();
      setPage(0);
    
      return financialData?.filter(
        (item) => 
        item.name.toLowerCase().includes(query)
      );
    }, [searched, financialData]);
  
    return (
        <Paper elevation={4}>
        <TableContainer>
        <Toolbar>
        <SearchBar
                placeholder="Search By Name"
                value={searched}
                onChange={(searchVal: string) => setSearched(searchVal)}
                onCancelSearch={() => setSearched('')}
              />
        </Toolbar>
        <Table>
          <TableHead>
            <TableRow>
              <TableCell>Symbol</TableCell>
              <TableCell>Name</TableCell>
              <TableCell>Currency</TableCell>
              <TableCell>Stock Exchange</TableCell>
              <TableCell>Exchange Short Name</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {filteredRows &&
            filteredRows.slice(page * PER_PAGE, page * PER_PAGE + PER_PAGE)
            .map((row,i) => (
              <FinancialDataRow
              key = {row.name}
              handleViewData={() => handleViewData(row)}
              symbol={row.symbol}
              name={row.name}
              currency={row.currency}
              stockExchange={row.stockExchange}
              exchangeShortName={row.exchangeShortName}
              />
            ))}
          </TableBody>
        </Table>
        </TableContainer>
      </Paper>
    );
  };
   
  export default Finance;

:您可以从下面看到,即使数据被列出了,因为它被返回,因为它被返回了,即使获取了数据:

I am currently trying to get data to display on my React App.

My query is set to:

interface ResultWrapper<T> {
result: T;
}
export interface FinancialData {
handleViewData: () => void;
symbol: string;
name: string;
stockExchange: string;
exchangeShortName: string;
currency: string;
}

export const useGetFinancialDataKey = 'get-financial-data';

const useGetFinancialData = (
  options: UseQueryOptions<FinancialData[], string> = {}
): UseQueryResult<FinancialData[], string> =>
   useQuery<FinancialData[], string>(
   useGetFinancialDataKey,
     async () =>
     (await axiosInstance.get<ResultWrapper<FinancialData[]>>. 
     ('https://financialmodelingprep.com/api/v3/search? 
     query=AA&limit=10&exchange=NASDAQ&apikey=ENTERAPIKEY'
     )).data.result, options
     );


export default useGetFinancialData;

I have checked the network tab, and I am getting a response back:
Network Tab

However, whenever I go to the home page, nothing displays and whenever I call the query in my finance.tsx file, I get an undefined when using

const {data: financialData} = useGetFinancialData();

The full file in finance.tsx looks something like this:

    const Finance: React.FC = () => {
    const {data: financialData} = useGetFinancialData();
    const [searched, setSearched] = useState<string>('');
    const [dataToView, setDataToView] = useState<FinancialData>();
    const [isViewDialogOpen, setIsViewDialogOpen] = useState(false);
  
    const [page, setPage] = useState(0);
    const PER_PAGE = 10;
  
    const handleViewData = useCallback(
      (data: FinancialData) => {
        setDataToView(data)
        setIsViewDialogOpen(true);
      },
      [setDataToView, setIsViewDialogOpen]
    );
    const filteredRows = useMemo(()=>{
      const query = searched.toLowerCase();
      setPage(0);
    
      return financialData?.filter(
        (item) => 
        item.name.toLowerCase().includes(query)
      );
    }, [searched, financialData]);
  
    return (
        <Paper elevation={4}>
        <TableContainer>
        <Toolbar>
        <SearchBar
                placeholder="Search By Name"
                value={searched}
                onChange={(searchVal: string) => setSearched(searchVal)}
                onCancelSearch={() => setSearched('')}
              />
        </Toolbar>
        <Table>
          <TableHead>
            <TableRow>
              <TableCell>Symbol</TableCell>
              <TableCell>Name</TableCell>
              <TableCell>Currency</TableCell>
              <TableCell>Stock Exchange</TableCell>
              <TableCell>Exchange Short Name</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {filteredRows &&
            filteredRows.slice(page * PER_PAGE, page * PER_PAGE + PER_PAGE)
            .map((row,i) => (
              <FinancialDataRow
              key = {row.name}
              handleViewData={() => handleViewData(row)}
              symbol={row.symbol}
              name={row.name}
              currency={row.currency}
              stockExchange={row.stockExchange}
              exchangeShortName={row.exchangeShortName}
              />
            ))}
          </TableBody>
        </Table>
        </TableContainer>
      </Paper>
    );
  };
   
  export default Finance;

As you can see from below, nothing is returned because it is being returned as undefined, even though the data is being fetched:
enter image description here

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

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

发布评论

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

评论(1

我一向站在原地 2025-02-17 10:11:27

谢谢@windowsill的帮助。

export interface FinancialData {
handleViewData: () => void;
symbol: string;
name: string;
stockExchange: string;
exchangeShortName: string;
currency: string;
}

export const useGetFinancialDataKey = 'get-financial-data';

const useGetFinancialData = (
options: UseQueryOptions<FinancialData[], string> = {}
 ): UseQueryResult<FinancialData[], string> => 
 useQuery<FinancialData[], string>(
  useGetFinancialDataKey,
 async () =>
(await axiosInstance.get<FinancialData[]>('https://financialmodelingprep.com/api/v3/search?query=AA&limit=10&exchange=NASDAQ&apikey=blahblah'
))
.data,
options

);


 export default useGetFinancialData;

thank you @windowsill for helping.

export interface FinancialData {
handleViewData: () => void;
symbol: string;
name: string;
stockExchange: string;
exchangeShortName: string;
currency: string;
}

export const useGetFinancialDataKey = 'get-financial-data';

const useGetFinancialData = (
options: UseQueryOptions<FinancialData[], string> = {}
 ): UseQueryResult<FinancialData[], string> => 
 useQuery<FinancialData[], string>(
  useGetFinancialDataKey,
 async () =>
(await axiosInstance.get<FinancialData[]>('https://financialmodelingprep.com/api/v3/search?query=AA&limit=10&exchange=NASDAQ&apikey=blahblah'
))
.data,
options

);


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