クリーンインストールしたら開発環境からNotionの接続情報も吹き飛んだ
1月2日ぶりにちょっと記事表示エリアが気持ち悪いなと思って。
cloneしてきてnpm run dev
あーpakcageのインストール忘れてたnpm i
これで動くんやろ?npm run dev
_人人人人人人人人人人人人人人人_
> DATABASE_ID is not defined <
 ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^ ̄
そう、何故かcloneから始まってる。
Windowsをクリーンインストールした時にWSL環境が吹っ飛んだのと同時に
.envに入れたNOTION_TOKENとDATABASE_IDが電子の海に飛んでいっていた。
NOTION_TOKENはNotionのサイトからノータイムで取って来ることができて事なきを得た。
今度は学習して1Passwordに記録したし完璧(この手のgitignoreする開発環境用の.envの情報って何で保持するのが良いんだろう。)
問題はDATABASE_ID、すこしググると「DBのページを開いたURLのXXXの部分がDATABASE_IDです。」と皆が口を揃えて説明している。AIにもその説明をされた。
しかし、適切にインテグレーションの設定をしてもうんともすんとも404
見えるところにある、DATABASE_IDっぽいもの全部試してみたけどダメ。
AIにキレたら、なんかよくわからないScriptを組んで持ってきてくれた。
const { Client } = require("@notionhq/client");
const dotenv = require("dotenv");
const path = require("path");
// Load .env.local from the root directory
dotenv.config({ path: path.resolve(process.cwd(), ".env.local") });
const notion = new Client({
auth: process.env.NOTION_TOKEN,
});
(async () => {
if (!process.env.NOTION_TOKEN) {
console.error("Error: NOTION_TOKEN is missing in .env.local");
process.exit(1);
}
console.log("Retrieving accessible databases...");
try {
const response = await notion.search({
sort: {
direction: "descending",
timestamp: "last_edited_time",
},
});
const databases = response.results.filter(
(item) => item.object === "database" || item.object === "data_source"
);
if (databases.length === 0) {
console.log("\nNo databases found.");
console.log(
"Please make sure you have connected your Notion Integration to the database:"
);
console.log("1. Open the database page in Notion.");
console.log("2. Click '...' > 'Connect to' > Select your integration.");
} else {
console.log("\nFound the following databases:\n");
databases.forEach((db) => {
console.log(`Title: ${db.title[0]?.plain_text || "Untitled"}`);
console.log(`ID: ${db.id}`); // This is the ID we need
console.log(`URL: ${db.url}`);
console.log("-".repeat(40));
});
console.log(
"\nCopy the 'ID' above and set it as DATABASE_ID in your .env.local file."
);
}
} catch (error) {
console.error("Failed to search databases:", error.message);
}
})();
たぶん見るにNOTION_TOKENで見れる情報ごそっと持ってきて、DATABASEかDATASOURCEがあれば表示してくれるというもの。
このスクリプトで取って来ることができたDATABASE_IDを使うとあら不思議、開発環境でブログ記事データの取得に成功した。
そしてその"正しいDATABASE_ID”をURLのXXXに入れて飛んでみたら404なんやねん。
俺のNotion術が低すぎて正しいページに飛べてないのかと思ったらちゃんと存在しない。
探し方は簡単だけど、AIなかったらこんなDATABASE_IDはできなかった。
さてそろそろこのぐちゃぐちゃのブログ記事表示エリアなんとかするか。
2026年1月19日 kouki