如何在 Nextjs 中使用 firestore 模拟器

How to use firestore emulator with Nextjs

我正在尝试在基于 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 连接吗?

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

使用 Firebase 版本。 9.4.1. 您可以使用数据库 UI 在本地使用数据库。 Url 到数据库 UI 显示,当启动模拟器时。 常规 url:本地主机:PORT/database