【问题标题】:NextJS Server not able to use firestoreNextJS 服务器无法使用 firestore
【发布时间】:2022-01-05 03:10:02
【问题描述】:

我有一些需要在 nextjs 服务器的前端和后端运行的功能。我正在尝试在服务器端使用 firebase 来处理一些敏感数据,但我无法为 firestore 集合文档创建 docref。我不断收到以下错误: 但我已经打印出有问题的值,它是有效的。下面是代码:

//page/api/firestore
import { doc } from "firebase/firestore";
import { db } from "lib/firestore";

export default async function handler(req, res) {
  try {
    const docRef = doc(db, "bots", "access_code");
  } catch (e) {
    console.log(e);
  }
  res.status(200).json({});
}

db 值定义如下:

//lib/firestore
export default initializeApp({
  apiKey: "**********",
  authDomain: "**********",
  projectId: "**********",
  storageBucket: "**********",
  messagingSenderId: "**********",
  appId: "**********",
});

export const db = getFirestore(app);

我正在前端做一些确切的事情,它工作正常????

【问题讨论】:

  • 能不能在服务端代码中console.log(db)查看是否定义?
  • 你有没有通过SO link?。似乎在您的情况下 docRef 也没有在客户端上执行
  • pages/api/firestore 是一个 API 路由,在服务器上执行。
  • @juliomalves 这就是问题所在,谢谢!我没有意识到 API 文件夹中的页面使用的是 nodejs,而不是 web 版本 9
  • @Badala Prashanth 我刚刚做了!

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


【解决方案1】:

我想通了! NextJS 在 api 目录中使用 NodeJS,但我在前端和后端都使用了 firebase Web 9 SDK。一旦我切换到 api 文件的 Node SDK,一切正常!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-04
    • 2020-07-14
    • 2023-02-12
    • 1970-01-01
    • 2021-07-23
    • 2020-08-23
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多