【问题标题】:How to use firestore emulator with Nextjs如何在 Nextjs 中使用 Firestore 模拟器
【发布时间】:2021-02-11 07:28:33
【问题描述】:

我正在尝试在基于 Nextjs 的本地项目中使用 Firebase 模拟器。按照 Firebase here 的指导,我在初始化 Firebase 后尝试使用 db.useEmulator('localhost', 8080);,但出现此错误:

Error was not caught TypeError: db.useEmulator is not a function
  at Module.eval (VM79706 firebase.js:30)
  at eval (VM79706 firebase.js:91)
  at Module../utils/firebase.js (_app.js?ts=1603918354205:23994)
  ...

我一直在使用模拟器来测试云功能,并且运行良好。我只是不明白如何将它与 Firestore 连接。

这是我设置 Firebase 的方法:

import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/storage';
import 'firebase/analytics';

const clientCredentials = {
  apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
  authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
  databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
  projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
  storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
  appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
  measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID
};

// Check that `window` is in scope for the analytics module!
if (typeof window !== 'undefined' && !firebase.apps.length) {
  firebase.initializeApp(clientCredentials);
  if ('measurementId' in clientCredentials) firebase.analytics();

  const db = firebase.firestore();
  if (process.env.NEXT_PUBLIC_DB_HOST === 'localhost') {
   db.useEmulator('localhost', 8080);
  }
}

export default firebase;

知道为什么会出现此错误以及如何将 Firebase 模拟器与 Nextjs 连接吗?

【问题讨论】:

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


    【解决方案1】:

    使用 Firebase 版本。 9.4.1。 您可以使用数据库 UI 在本地使用数据库。启动模拟器时显示数据库 UI 的 URL。 常规网址:localhost:PORT/database

    【讨论】:

    • 您是否设法让模拟器与 Next.js 一起工作?我刚得到,Firestore has already been started and its settings can no longer be changed.
    【解决方案2】:

    回答我自己的问题,问题是这个方法是在新版本的firebase 8.0.0中引入的。我刚刚收到更新,瞧!

    【讨论】:

    • 我在 firebase --version > 8.15.0 上,我仍然得到函数。useEmulator 不是函数
    猜你喜欢
    • 2020-08-30
    • 2020-11-07
    • 2020-06-18
    • 2019-12-23
    • 2020-01-07
    • 1970-01-01
    • 2020-12-06
    • 2020-03-14
    • 2020-10-21
    相关资源
    最近更新 更多