得到“未定义”使用 NextJS 获取 Opensea/Rarible API 时

发布于 2025-01-18 09:23:33 字数 1584 浏览 0 评论 0原文

I have the following:

// FetchData.js

export const fetchData = async (url, options) => {
    const res = await fetch(url, {
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
          },
        ...options,
    })
    const data = await res.json()
    return data
}
// prototype.jsx

import { fetchData } from '../../lib/FetchData'

const Prototype = ({ data }) => {
    console.log('OpenSea API', data)
    return ( 
        <div className="h-screen">
            Prototype page
            {data}
        </div>
     );
}
 
export default Prototype;

export async function getSeverSideProps(context) {
    // retrieve assets from OpenSea API
    const url = `https://api.opensea.io/api/v1/assets?owner=0xa48db0a225703b25ef95b863c1aa44929bba7fde&order_direction=desc&offset=0&limit=20`;
    // const url = `https://api.opensea.io/api/v1/collections?asset_owner=0x9f5499EfaeD12770762d44261538151b47E8d902&offset=0&limit=300&offset=0&limit=300`
    // const url = `https://api.rarible.org/v0.1/collections/ETHEREUM:0x1eeabf7196c6610a3336add6d5f1a178c0734577`
    const options = {method: 'GET'};
    const data = await fetchData(url, options)

    return {
        props: {
            data
        }
    }
}

When I console logged from the page, it is "undefined".我推测这可能是因为我需要从Opensea请求一个API键,因此我没有尝试用稀有的API获取API键,但它仍然不确定。

我曾在Opensea和稀有API上尝试过Postman的API,并且都收到了具有200个状态的JSON数据。但是,当NextJs在NextJ中进行了尝试时,它会变得不确定。

I have the following:

// FetchData.js

export const fetchData = async (url, options) => {
    const res = await fetch(url, {
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
          },
        ...options,
    })
    const data = await res.json()
    return data
}
// prototype.jsx

import { fetchData } from '../../lib/FetchData'

const Prototype = ({ data }) => {
    console.log('OpenSea API', data)
    return ( 
        <div className="h-screen">
            Prototype page
            {data}
        </div>
     );
}
 
export default Prototype;

export async function getSeverSideProps(context) {
    // retrieve assets from OpenSea API
    const url = `https://api.opensea.io/api/v1/assets?owner=0xa48db0a225703b25ef95b863c1aa44929bba7fde&order_direction=desc&offset=0&limit=20`;
    // const url = `https://api.opensea.io/api/v1/collections?asset_owner=0x9f5499EfaeD12770762d44261538151b47E8d902&offset=0&limit=300&offset=0&limit=300`
    // const url = `https://api.rarible.org/v0.1/collections/ETHEREUM:0x1eeabf7196c6610a3336add6d5f1a178c0734577`
    const options = {method: 'GET'};
    const data = await fetchData(url, options)

    return {
        props: {
            data
        }
    }
}

When I console logged from the page, it is "undefined". I surmised that it may be because I need to request an API key from OpenSea, so instead of I have tried fetching with Rarible API and it remains undefined.

I have tried by OpenSea and Rarible API on Postman and both received a JSON data with status 200. But when tried it in NextJS, it keeps getting undefined.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文