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

Supabase查询返回对象而非Array,求原因及解决方法

问题原因与解决方法

Supabase JavaScript客户端的select()查询返回的响应对象结构是固定的:查询结果数组存储在data字段中,而非你自定义的books字段。你当前的代码错误地解构了不存在的books属性,导致最终得到的books要么是undefined,要么是整个响应对象,因此原型显示为Object而非Array。

修改后的代码

export async function getStaticProps() {
    var today = new Date();
    today = today.toISOString().split('T')[0];

    // 解构响应中的data字段,并将其重命名为books
    const { data: books, error } = await supabase.from('books')
        .select('*')
        .gt('published', today)
        .limit(10)
        .order('published', { ascending: true });

    if (error) {
        throw new Error(error);
    }
    
    return {
        props: {
            books
        }
    };
}

修改后,books将成为查询返回的数组,原型为Array,你可以正常使用map等数组方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 17:52:45