【问题标题】:Update function to Firebase V9 with nextjs使用 nextjs 将函数更新到 Firebase V9
【发布时间】:2022-01-24 08:14:13
【问题描述】:

我已经用 Firebase 版本 9 更新了我的项目,但我开始遇到一些我仍然无法解决的代码错误。

之前的函数是这样的。我需要为新的 firebase V9 版本更新它

const doc = await firebaseApp
  .firestore()
  .collection('documents')
  .doc(params.documento)
  .get();

if (!doc) {
  return {
    notFound: true,
  };
}

const data = doc.data();

return {
  props: {
    texto: {
      ...data,
      fecha: data?.fecha?.toDate().toLocaleString('es-ES', { timeZone: 'UTC', day: '2-digit', month: 'long', year: 'numeric' }),
    },
  },
};
}



【问题讨论】:

  • 与其发布您的 V8 代码并仅仅要求别人为您将其翻译成 V9,最好展示您对 V9 代码的尝试,然后描述您遇到的错误。
  • Firebase documentation 有 v8 和 v9 语法的代码示例,因此您可以比较它们,upgrade guide 也概述了该过程。
  • 是的,错误是 A required parameter (documento) was not provided as a string in getStaticPaths for /documento/[documento] 当前代码是 `export const getStaticPaths = async () => { const快照 = 等待 getDocs (collection(db, 'documents')); const paths = snapshot.docs.map(doc => { return { params: {id: doc.id.toString()} } }) return { paths, fallback: false } } `

标签: javascript reactjs firebase google-cloud-firestore next.js


【解决方案1】:

我找到了解决这个问题的方法。


    const snapshot = await getDocs (collection(db, 'documents')); 
    const paths = snapshot.docs.map(doc => {
      
      return {
        params: {
          documento: doc.id.toString()
        }
      }
    })

    return {
      paths,
      fallback: false
    }
}

export const getStaticProps = async (context) => {
  const documento = context.params.documento;

  const docRef = doc(db, 'documents', documento);
  const docSnap = await getDoc(docRef);

  return {

    props: {textoProps: JSON.stringify(docSnap.data()) || null}

  }
}

【讨论】:

    猜你喜欢
    • 2022-11-27
    • 2022-06-11
    • 2022-11-30
    • 1970-01-01
    • 2022-12-15
    • 2022-01-12
    • 1970-01-01
    • 2021-12-11
    • 2021-06-10
    相关资源
    最近更新 更多