如何在Next js,typescript中获取这些数据?[关闭]

cu6pst1q  于 2023-06-22  发布在  TypeScript
关注(0)|答案(1)|浏览(112)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答复。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
2天前关闭。
Improve this question
它们都在不同的文件中。我得到的错误是Unhandled Runtime Error Error:userData.map不是函数

getAllData.tsx

import axios, { AxiosError } from "axios";

export default async function getAllData() {
  const url = "https://search-practice-backend.vercel.app/api/v1/search";
  try {
    const { data } = await axios.get(url);
    return data as AllData[];
  } catch (error: unknown) {
    if (error instanceof AxiosError) {
      console.log(error.response);
    }
    throw error;
  }
}

types.d.ts

type AllData = {
  _id: string;
  title: string;
  cost: number;
  promote: boolean;
  grade: number;
  madeAt: string;
  seller: string;
};

**DisplayFullData.tsx *

import getAllData from "@/lib/getAllData";

export const DisplayFullData = async () => {
  const userData: AllData[] = await getAllData();
  console.log("Hello");

  const content = (
    <main>
      {userData.map((item) => {
        return (
          <>
            <h4 key={item._id}>{item.title}</h4>
          </>
        );
      })}
    </main>
  );
  return content;
};
import styles from "./page.module.css";
import { DisplayFullData } from "./components/DisplayFullData";

export default function Home() {
  return (
    <main className={styles.main}>
      <DisplayFullData />
    </main>
  );
}

这是正在尝试提取的数据的结构

{"final":[{"_id":"648d124e7111d378381e1d6d","title":"Movie","cost":452,"promote":true,"grade":3,"madeAt":"2023-06-17T01:54:17.725Z","seller":"big mike","__v":0},{"_id":"648d124e7111d378381e1d6e","title":"Clock","cost":5,"promote":false,"grade":2,"madeAt":"2023-06-17T01:54:17.725Z","seller":"don","__v":0},{"_id":"648d124e7111d378381e1d69","title":"Cup","cost":306,"promote":false,"grade":1,"madeAt":"2023-06-17T01:54:17.725Z","seller":"made","__v":0},{"_id":"648d124e7111d378381e1d6c","title":"Donkey","cost":200,"promote":true,"grade":5,"madeAt":"2023-06-17T01:54:17.725Z","seller":"big mike","__v":0},{"_id":"648d124e7111d378381e1d6a","title":"Bike","cost":456,"promote":false,"grade":1,"madeAt":"2023-06-17T01:54:17.725Z","seller":"ck ventures","__v":0},{"_id":"648d124e7111d378381e1d68","title":"Jacket u-model","cost":29,"promote":false,"grade":3,"madeAt":"2023-06-17T01:54:17.725Z","seller":"don","__v":0},{"_id":"648d124e7111d378381e1d63","title":"Bag","cost":74,"promote":true,"grade":3,"madeAt":"2023-06-17T01:54:17.725Z","seller":"ck ventures","__v":0},{"_id":"648d124e7111d378381e1d64","title":"Shoe","cost":144,"promote":false,"grade":1,"madeAt":"2023-06-17T01:54:17.725Z","seller":"ck ventures","__v":0},{"_id":"648d124e7111d378381e1d67","title":"Jacket y-model","cost":20,"promote":false,"grade":4,"madeAt":"2023-06-17T01:54:17.725Z","seller":"don","__v":0},{"_id":"648d124e7111d378381e1d6b","title":"Camel","cost":456,"promote":true,"grade":1,"madeAt":"2023-06-17T01:54:17.725Z","seller":"big mike","__v":0}],"nbHits":10}
x6h2sr28

x6h2sr281#

错误发生在getAllData()中。它应该返回data.final而不是数据

相关问题