You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js 13使用getStaticParams获取Supabase数据时遇TypeError错误

问题:Next.js 13 + Supabase 详情页数据获取失败(TypeError: result is not iterable)

我使用Next.js 13的新数据获取方法从Supabase数据库拉取数据,列表页能正常获取用户数据,点击用户卡片后URL也能切换到预期路径,但用户详情页无法获取数据,报错:

TypeError: result is not iterable

列表页代码

export const revalidate = 60;

async function getData() {
  const { data } = await supabase.from("profiles").select().eq("is_host", true);
  return { data };
}

export default async function Hosts() {
  const data = await getData();

  return ( 
    <div> 
      {data.data!.map((profile) => ( 
        <div key={profile.id}>
          <Link href={`/hosts/${profile.username}`}></Link>
        </div>
      ))} 
    </div>
  );
}

用户详情页代码

export const revalidate = 60;

export async function generateStaticParams() {
  const { data: profiles } = await supabase.from("profiles").select("username");

  const hostPath = profiles?.map((profile) => ({
    params: { host: profile.username },
  }));
  
  return {
    hostPath,
    fallback: false,
  };
}

export default async function HostPage({ params: { username } }: { params: { username: string } }) {
  const { data: profile } = await supabase.from('profiles').select().match({ username }).single();

  return (
    <div>
      <pre>{JSON.stringify(profile, null, 2)}</pre>
    </div>
  );
}

注:两个页面均为服务端渲染。

更新: 移除获取数据方法返回语句中data的{}包裹后,一切正常(我之前将数组包裹在对象中,导致无法遍历)。

内容的提问来源于stack exchange,提问作者nelakay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 01:50:16