next.js Vercel Edge运行时的概念API响应400

oaxa6hgo  于 2023-01-08  发布在  其他
关注(0)|答案(1)|浏览(186)

来自Vercel Edge Runtime上Next.js的API路径,概念API,例如:Retrieve a databaseQuery a database,如下响应400无效请求URL:

{"object":"error","status":400,"code":"invalid_request_url","message":"Invalid request URL."}

从本地环境(next devvercel dev),概念API正确地响应200。
代码如下:

检索数据库

import { NextRequest } from 'next/server'

export const config = { runtime: 'edge' }

async function(req: NextRequest) {
  const url = `https://api.notion.com/v1/databases/${DATABASE_ID}`
  const headers = {
      'Authorization': `Bearer ${NOTION_API_SECRET}`,
      'Notion-Version': '2022-06-28',
      'Content-Type': 'application/json',
    }

  const response = await fetch(url, {
    method: 'GET',
    headers: headers,
  })

  const data = await response.json()
  console.log(JSON.stringify(data))
}

查询数据库

import { NextRequest } from 'next/server'

export const config = { runtime: 'edge' }

async function(req: NextRequest) {
  const { searchParams } = new URL(req.url);

  if (!searchParams.has('slug')) {
    throw new Error('No slug in searchParams.')
  }

  const slug = searchParams.get('slug').trim()

  const url = `https://api.notion.com/v1/databases/${DATABASE_ID}/query`
  const body = JSON.stringify({
      filter: {
        and: [
          { property: 'Published', checkbox: { equals: true } },
          { property: 'Slug', rich_text: { equals: slug } },
        ],
      },
    })
  const headers = {
      'Authorization': `Bearer ${NOTION_API_SECRET}`,
      'Notion-Version': '2022-06-28',
      'Content-Type': 'application/json',
    }

  const response = await fetch(url, {
    method: 'POST',
    body: body,
    headers: headers,
  })

  const data = await response.json()
  console.log(JSON.stringify(data))
}

NOTION_API_SECRETDATABASE_ID设置正确。
200是预期的,但不是。
我尝试的事情如下:

okxuctiv

okxuctiv1#

我在使用节点包时遇到了同样的问题,我将我的功能从Edge切换回了Normal,部署时它正在工作。

export default async function handler(req, res) {
  const response = await getPosts();

  const scheme = response.scheme;
  const items = response.items;
  res.status(200).json({
    scheme,
    items,
  })
}

getPosts()函数定义

import { Client, isFullDatabase } from "@notionhq/client";
import { type } from "os";
const notion = new Client({
  auth: process.env.NOTION_SECRET,
});

const processDatabase = (database) => {
  const notionDatabase = { scheme: "test", items: [] };
  database.results.forEach((result) => {
    const properties = result.properties;
    const row = {
      id: result.id,
      data: {
        name: properties.Name.title[0].plain_text,
        author: properties.Author.rich_text[0].plain_text,
        status: properties.Status.multi_select[0].name,
        start_date: properties.Start.date?.start,
        end_date: properties.End.date?.start,
      },
    };
    notionDatabase.items.push(row);
  });
  return notionDatabase;
};
export async function getPosts() {
  const response = await notion.databases.query({
    database_id: "",
    sorts: [
      {
        property: "Status",
        direction: "ascending",
      },
    ],
  });
  console.log(response);
  return processDatabase(response);
}

我的边缘功能上一次工作是在34天前部署到vercel。我的下一次部署是在26天前,不再工作并返回500。从我的构建日志来看,所有的变化似乎是Vercel CLI版本从28.6.0到28.8.0,不确定这是否重要。

相关问题