首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firestore在Next.js应用程序中运行时,一直试图安装模拟器

Firestore在Next.js应用程序中运行时,一直试图安装模拟器
EN

Stack Overflow用户
提问于 2021-10-18 17:35:39
回答 2查看 460关注 0票数 0

有没有人遇到过火基地及其仿真器的麻烦?在热重装时,我一直收到以下错误。

FirebaseError: Firestore已经启动,其设置无法再更改。您只能在调用Fi还原对象.上的任何其他方法之前修改设置。

初始化:

代码语言:javascript
复制
// Initialize Firebase
const firebaseApp = initializeApp(firebaseConfig);

let mode = "emulate";

const auth = getAuth(firebaseApp);
const db = getFirestore(firebaseApp);
const storage = getStorage(firebaseApp);
const functions = getFunctions(firebaseApp);

if (mode === "emulate") {
  connectAuthEmulator(auth, "http://localhost:9099");
  connectFirestoreEmulator(db, "localhost", 8080);
  connectStorageEmulator(storage, "localhost", 9199);
  connectFunctionsEmulator(functions, "localhost", 5001);
}

export { firebaseApp, db, auth, storage, functions };
EN

回答 2

Stack Overflow用户

发布于 2021-10-22 18:47:34

似乎在同一个浏览器实例中多次调用useEmulator。我的建议是检查这个post,其中类似的问题。

票数 1
EN

Stack Overflow用户

发布于 2022-03-24 10:55:55

我将在这里添加this post的解决方案,因为它对我有用。

解决方案

代码语言:javascript
复制
    const EMULATORS_STARTED = 'EMULATORS_STARTED';
    function startEmulators() {
      if (!global[EMULATORS_STARTED]) {
        global[EMULATORS_STARTED] = true;
        firebase.firestore().useEmulator('localhost', <your_port>);
        firebase.auth().useEmulator('http://localhost:<your_port>/');
      }
    }
    
    if (process.env.NODE_ENV === 'development') {
      startEmulators();
    }

注意-如果您正在使用Firebase V9:

代码语言:javascript
复制
const EMULATORS_STARTED = 'EMULATORS_STARTED';
function startEmulators() {
  if (!global[EMULATORS_STARTED]) {
    global[EMULATORS_STARTED] = true;
    connectFunctionsEmulator(functions, 'localhost', <your_port>);

    connectFirestoreEmulator(db, 'localhost', <your_port>);
  }
}

if (process.env.NODE_ENV === 'development') {
  startEmulators();
}

原因:

之所以需要这样做,是因为NextJS试图多次运行到模拟器的连接。这样可以防止仿真器不止一次连接。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69620265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档