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

Notion数据库查询API返回500错误,无法获取50条以上数据

解决Notion Query Database API无法获取50条以上记录的问题

可行解决方案

1. 固定使用page_size=50并实现分页循环

官方文档标注page_size最大支持100,但你的场景下设置51及以上触发500错误,建议固定使用50作为每页条数,通过next_cursor循环获取所有数据,同时添加错误重试机制处理临时内部错误:

const { Client } = require('@notionhq/client');
const notion = new Client({ auth: process.env.NOTION_API_KEY });

async function fetchAllEntries(databaseId) {
  let allResults = [];
  let cursor = null;

  do {
    try {
      const res = await notion.databases.query({
        database_id: databaseId,
        page_size: 50,
        start_cursor: cursor,
        // 按需添加你的filter和sort参数,确保语法合规
        filter: {
          property: 'Status',
          status: { equals: 'Completed' }
        },
        sort: [{ property: 'Updated', direction: 'descending' }]
      });

      allResults.push(...res.results);
      cursor = res.next_cursor;
    } catch (err) {
      if (err.code === 'internal_server_error') {
        console.log('服务器内部错误,1秒后重试...');
        await new Promise(r => setTimeout(r, 1000));
        continue;
      }
      throw err;
    }
  } while (cursor);

  return allResults;
}

// 调用示例
fetchAllEntries('your-database-uuid')
  .then(data => console.log(`共获取${data.length}条记录`))
  .catch(e => console.error('请求失败:', e));

2. 简化Filter/Sort逻辑

复杂的多条件过滤或排序组合可能加重服务器计算负载,触发内部错误:

  • 拆分复杂filter为多个独立请求,后续在本地合并结果
  • 减少排序条件数量,优先使用数据库已索引的属性排序

3. 临时重试策略

500内部错误有时是Notion服务器的临时波动,添加指数退避重试(比如第一次等1秒,第二次等2秒,最多重试3次)可提升成功率。

问题原因分析

你遇到的internal_server_error通常和以下因素相关:

  • 请求参数组合超出Notion服务器的处理能力(如大page_size+复杂filter/sort)
  • 部分数据库场景下,官方标注的page_size=100上限实际不可用,需降为50
  • next_cursor传递过程中出现参数错误(需确保每次请求的page_size与首次一致)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 08:47:47