简体   繁体   English

Nextjs getStaticPaths 不从 firebase firestore 获取数据

[英]Nextjs getStaticPaths not fetching data from firebase firestore

I am failing to get dynamic data from firestore using getStaticPaths in nextjs.我无法在 nextjs 中使用 getStaticPaths 从 firestore 获取动态数据。 When I render the data from firestore using getStaticProps, it works, but when I open a specific item to get its details, it refuses and gives me a 404 page.当我使用 getStaticProps 从 firestore 渲染数据时,它可以工作,但是当我打开特定项目以获取其详细信息时,它拒绝并给我一个 404 页面。 This is what my code looks like for now, the [id].js page.这就是我的代码现在的样子,[id].js 页面。

import React from 'react'
import { db } from '@/Firebase';
import {collection, getDoc} from "firebase/firestore";

const reference = collection(db, "abantu");


export const getStaticProps = async (context) => {
    const id = context.params.id;
    const data = await getDoc(reference);
    const umuntuData = fetch(`${data}` + id);
    
    return {
        props: {
            umuntu: umuntuData
        }
    }
}

export const getStaticPaths= async () => {
    const umuntu = await getDoc(reference);
    // const umuntuData = umuntu.docs

    const paths = umuntu.docs.map(doc => {
        return {
            params: { id: doc.id }
        }
    })

    return {
        paths,
        fallback: false
    }

}
function Details({umuntu}) {
  return (
    <div>
        <h1>{umuntu.ibizo}</h1>
    </div>
  )
}

export default Details

Where could I be going wrong?.我哪里会出错?

Your query getDoc(specific doc) vs getDocs(list of docs)您的查询 getDoc(specific doc) vs getDocs(list of docs)

export const getStaticPaths= async () => {
    const umuntu = await getDocs(reference);
    // const umuntuData = umuntu.docs

    const paths = umuntu.docs.map(doc => {
        return {
            params: { id: doc.id }
        }
    })

    return {
        paths,
        fallback: false
    }

}

For your static props, you will need to get specific document对于您的 static 道具,您需要获得特定文件

//import {doc} from "firebase/firestore";
export const getStaticProps = async (context) => {
    const id = context.params.id;
    const docRef = doc(db, "abantu", id);
    const data = await getDoc(docRef);
    const umuntuData = fetch(`${data}` + id);
    
    return {
        props: {
            umuntu: umuntuData
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 nextjs 中的 getStaticPaths 从 firestore 获取动态数据 - Failing to fetch dynamic data from firestore using getStaticPaths in nextjs 使用 flutter 从 firebase Firestore 获取数据 - fetching data from firebase firestore using flutter 如何使用 NextJs API、Firebase Firestore、axios 和 TypeScript 从 Firebase 集合中获取数据? - How do I get data from Firebase collection using NextJs API, Firebase Firestore, axios and TypeScript? Firebase Firestore 使用 ID 引用获取数据然后获取引用 - Firebase Firestore fetching data with an ID reference then fetching the reference React Native Firebase Firestore 数据未正确获取 - React Native Firebase Firestore data not fetching properly 从 firestore 获取和 Firebase 函数超时 - Fetching from firestore and Firebase functions timeout 正在获取 Firebase Firestore 引用 - Fetching Firebase Firestore References 无法使用 nextjs 从 firestore 读取数据 - Failing to read data from firestore using nextjs 从firestore(firebase)获取数据时出现错误。 我正在使用本机反应 - I am getting an error while fetching data from firestore(firebase). I am using react native 当从 firestore 的 firebase 云 function 中获取数据时,我们在使用转换器时得到的是不可分配的错误 - When fetching data in a firebase cloud function from firestore we get is not assignable errors when using a converter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM