Skip to content

Commit e920af7

Browse files
committed
Add debug API to test MongoDB queries
1 parent c7dbc33 commit e920af7

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

api/debug-posts.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { VercelRequest, VercelResponse } from '@vercel/node';
2+
import { MongoClient } from 'mongodb';
3+
4+
const uri = process.env.MONGODB_URI;
5+
6+
export default async function handler(req: VercelRequest, res: VercelResponse) {
7+
if (!uri) {
8+
console.error('MONGODB_URI environment variable is not set');
9+
res.status(500).json({ error: 'Database configuration error' });
10+
return;
11+
}
12+
13+
try {
14+
const client = new MongoClient(uri);
15+
await client.connect();
16+
17+
const db = client.db('blog_database');
18+
const postsCollection = db.collection('posts');
19+
20+
// Test different queries
21+
const allPosts = await postsCollection.find({}).limit(5).toArray();
22+
const publishedPosts = await postsCollection.find({ isPublished: true }).limit(5).toArray();
23+
const draftPosts = await postsCollection.find({ draft: { $ne: true } }).limit(5).toArray();
24+
25+
await client.close();
26+
27+
res.status(200).json({
28+
totalPosts: allPosts.length,
29+
publishedPosts: publishedPosts.length,
30+
draftPosts: draftPosts.length,
31+
samplePublished: publishedPosts.map(p => ({ title: p.title, isPublished: p.isPublished, draft: p.draft })),
32+
sampleAll: allPosts.map(p => ({ title: p.title, isPublished: p.isPublished, draft: p.draft }))
33+
});
34+
35+
} catch (error) {
36+
console.error('Error debugging posts:', error);
37+
res.status(500).json({ error: 'Error debugging posts' });
38+
}
39+
}

0 commit comments

Comments
 (0)