我正在使用部署在 Netlify 上的无服务器函数来使用内部集成令牌从概念数据库中获取数据,并尝试使用以下方法获取数据:
const promise = fetch('<deployed website url on netlify>/.netlify/functions/notion-proxy');
promise.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
无服务器功能已实现,因为我在 catch 语句中收到自定义错误(“内部服务器错误”)并且正文中有一条错误消息:“确保相关页面和数据库与您的集成共享” - 即使我有已经与集成共享数据库。我检查了集成是否有访问数据库的适当权限,我也尝试再次与集成共享数据库,但错误仍然存在。
这是无服务器函数的代码:
import { Client } from "@notionhq/client";
const apiKey = process.env.API_KEY;
const databaseId = process.env.DATABASE_ID;
const notion = new Client({ auth: apiKey });
const username = ""; // Notion username
const handler = async (event) => {
try {
const response = await notion.databases.query({
database_id: databaseId,
filter: {
property: "ID",
rich_text: {
contains: username,
},
},
});
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
},
body: JSON.stringify(response),
};
} catch (error) {
console.log("error.body: ", error.body);
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
},
body: JSON.stringify({
error: "Internal server error",
errorBody: error.body
}),
};
}
};
module.exports = { handler };
我还能做些什么来解决这个问题?
我已经更新了代码,现在可以实现无服务器功能,但我仍然无法从概念数据库中获取数据——错误要求我检查集成的权限,尽管我已经添加了所有权限(稍后我想在Webflow上查询抓取的数据)