Firebase Firestore可与实际数据库一起使用,但不能与模拟器一起使用 [英] Firebase Firestore works with real database but not emulator

查看:86
本文介绍了Firebase Firestore可与实际数据库一起使用,但不能与模拟器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase,并且已经开始尝试使用Firebase模拟器运行本地开发实例.我正在运行Firebase Tools v8.4.1.当我使用真实的Firestore数据库运行时,一切正常.但是,当我使用仿真器时出了点问题.

I'm using Firebase and have started trying to run a local development instance using the Firebase Emulators. I'm running v8.4.1 of Firebase Tools. When I'm running with the real Firestore database everything works fine. However there is something wrong when I'm using the emulators.

当我通过Firebase模拟器添加数据时,我可以设置一个文档,然后通过Javascript 获取,但是在Firebase Emulators UI中没有任何显示,反之亦然在UI中,使用Javascript中的 get 时不可用.另外,当我使用Firebase Tools CLI的导入和导出功能时,我通过Javascript添加的所有数据都不会导出和导入.我在浏览器控制台中没有任何错误.

I can set a document and then get it through Javascript but nothing shows in the Firebase Emulators UI and the same in reverse, when I add data through the Firebase Emulators UI it isn't available using get in Javascript. Also, when I use the import and export function of the Firebase Tools CLI none of the data I've added through Javascript is exported and imported. I'm not getting any errors in the browser console.

我不确定初始化Firebase或我缺少的其他一些配置时是否做错了什么?

I'm not sure if there is something wrong I'm doing in initializing Firebase or some other config I'm missing?

我正在运行: firebase模拟器:start --only功能,firestore

Firebase设置代码

import * as firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/functions';

const db = firebase.initializeApp({
  apiKey: 'my_api_key',
  appId: 'my_app_id',
  authDomain: 'my_auth_domain',
  databaseURL: 'my_database_url',
  messagingSenderId: 'my_messaging_sender_id',
  projectId: 'my_project_id',
  storageBucket: 'my_storage_bucket',
}).firestore();

if (process.env.NODE_ENV === 'development') {
  firebase.setLogLevel('debug');

  db.settings({
    host: 'localhost:8000',
    ssl: false,
  });

  firebase.functions().useFunctionsEmulator('http://localhost:5001');
}

export { db };

firebase.json

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "source": "packages/functions"
  },
  "hosting": {
    "public": "packages/frontend/dist",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },
  "storage": {
    "rules": "storage.rules"
  },
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8000
    },
    "ui": {
      "enabled": true
    }
  }
}

浏览器控制台

index.esm.js?abfd:106 [2020-05-30T03:18:23.148Z]  @firebase/firestore: Firestore (7.14.5): FirestoreClient Initializing. user= Jgjhmnb23JHFDTRGEYK5Jppwed
index.esm.js?abfd:106 [2020-05-30T03:18:23.162Z]  @firebase/firestore: Firestore (7.14.5): MemoryPersistence Starting transaction: Get last stream token
index.esm.js?abfd:106 [2020-05-30T03:18:23.164Z]  @firebase/firestore: Firestore (7.14.5): MemoryPersistence Starting transaction: Get next mutation batch
index.esm.js?abfd:106 [2020-05-30T03:18:23.166Z]  @firebase/firestore: Firestore (7.14.5): MemoryPersistence Starting transaction: Allocate target
index.esm.js?abfd:106 [2020-05-30T03:18:23.167Z]  @firebase/firestore: Firestore (7.14.5): MemoryPersistence Starting transaction: Execute query
index.esm.js?abfd:106 [2020-05-30T03:18:23.168Z]  @firebase/firestore: Firestore (7.14.5): IndexFreeQueryEngine Using full collection scan to execute query: Query(target=Target(categories, orderBy: [name (asc), __name__ (asc)]); limitType=F)
index.esm.js?abfd:106 [2020-05-30T03:18:23.173Z]  @firebase/firestore: Firestore (7.14.5): Connection Creating WebChannel: http://localhost:8000/google.firestore.v1.Firestore/Listen/channel [object Object]
index.esm.js?abfd:106 [2020-05-30T03:18:23.178Z]  @firebase/firestore: Firestore (7.14.5): Connection Opening WebChannel transport.
index.esm.js?abfd:106 [2020-05-30T03:18:23.180Z]  @firebase/firestore: Firestore (7.14.5): Connection WebChannel sending: {"database":"projects/vendida-a17e6/databases/(default)","addTarget":{"query":{"structuredQuery":{"from":[{"collectionId":"categories"}],"orderBy":[{"field":{"fieldPath":"name"},"direction":"ASCENDING"},{"field":{"fieldPath":"__name__"},"direction":"ASCENDING"}]},"parent":"projects/my-project-ha47s/databases/(default)/documents"},"targetId":2}}
index.esm.js?abfd:106 [2020-05-30T03:18:23.217Z]  @firebase/firestore: Firestore (7.14.5): Connection WebChannel transport opened.
index.esm.js?abfd:106 [2020-05-30T03:18:23.241Z]  @firebase/firestore: Firestore (7.14.5): Connection WebChannel received: {"targetChange":{"targetChangeType":"ADD","targetIds":[2]}}
index.esm.js?abfd:106 [2020-05-30T03:18:23.242Z]  @firebase/firestore: Firestore (7.14.5): Connection WebChannel received: {"targetChange":{"targetChangeType":"CURRENT","targetIds":[2],"resumeToken":"HGfhjBG7dfU56Gn=","readTime":"2020-05-30T03:18:23.208926Z"}}
index.esm.js?abfd:106 [2020-05-30T03:18:23.242Z]  @firebase/firestore: Firestore (7.14.5): Connection WebChannel received: {"targetChange":{"resumeToken":"YFgT/hjfF76fWj1=","readTime":"2020-05-30T03:18:23.209597Z"}}
index.esm.js?abfd:106 [2020-05-30T03:18:23.244Z]  @firebase/firestore: Firestore (7.14.5): MemoryPersistence Starting transaction: Get last remote snapshot version
index.esm.js?abfd:106 [2020-05-30T03:18:23.246Z]  @firebase/firestore: Firestore (7.14.5): MemoryPersistence Starting transaction: Apply remote event
index.esm.js?abfd:106 [2020-05-30T03:18:23.248Z]  @firebase/firestore: Firestore (7.14.5): MemoryPersistence Starting transaction: notifyLocalViewChanges

firestore-debug.log

firestore-debug.log

May 30, 2020 1:08:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
May 30, 2020 1:09:06 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
May 30, 2020 1:09:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.

firebase-debug.log

firebase-debug.log

[debug] [2020-05-30T03:10:25.924Z] May 30, 2020 1:10:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"May 30, 2020 1:10:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}}
[debug] [2020-05-30T03:11:06.713Z] May 30, 2020 1:11:06 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"May 30, 2020 1:11:06 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}}
[debug] [2020-05-30T03:11:25.949Z] May 30, 2020 1:11:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"May 30, 2020 1:11:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}}

推荐答案

原来,该问题是由于Firebase Tools的当前状态引起的.我以前一直在将Firebase Tools与另一个项目一起使用,并且必须对要与以下模拟器一起使用的当前项目运行 use 命令:

Turns out the issue was due to the current status of Firebase Tools. I had previously been using Firebase Tools with another project and had to run the use command for my current project that I was trying to use the emulators with:

firebase use project_name

现在一切正常.

这篇关于Firebase Firestore可与实际数据库一起使用,但不能与模拟器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆