Fix: Add ID fallback for news detail route
This commit is contained in:
parent
48ad517133
commit
eed1bc1b6b
@ -5,17 +5,25 @@ import { GlobalSidebar } from '@/components/AdWrappers';
|
|||||||
import { Calendar, User, ArrowLeft } from 'lucide-react';
|
import { Calendar, User, ArrowLeft } from 'lucide-react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
|
|
||||||
async function fetchPostData(slug: string) {
|
async function fetchPostData(slugOrId: string) {
|
||||||
try {
|
try {
|
||||||
const client = await createAdminClient();
|
const client = await createAdminClient();
|
||||||
|
const dbId = APPWRITE_ALMEIRA_CONFIG.databaseId;
|
||||||
|
|
||||||
const res = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'posts', [
|
// 1. Try by slug
|
||||||
Query.equal('slug', slug),
|
let res = await client.databases.listDocuments(dbId, 'posts', [
|
||||||
|
Query.equal('slug', slugOrId),
|
||||||
Query.limit(1)
|
Query.limit(1)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (res.documents.length === 0) return null;
|
if (res.documents.length > 0) return res.documents[0];
|
||||||
return res.documents[0];
|
|
||||||
|
// 2. Try by $id (fallback)
|
||||||
|
try {
|
||||||
|
return await client.databases.getDocument(dbId, 'posts', slugOrId);
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Post detail fetch error", e);
|
console.error("Post detail fetch error", e);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user