firebase 无法写入Firestore Emulator

wlsrxk51  于 2023-04-07  发布在  其他
关注(0)|答案(2)|浏览(150)

我正在使用Firebase和React。我无法写入我的模拟器数据,或者更确切地说,我在写入后看不到它。因此,当用户注册时,我将其名称复制到Firestore,因此即使Web应用程序给出适当的通知,也没有向http://127.0.0.1:4000/firestore添加任何内容。我想为函数,firestore,托管,认证和存储,但只有认证在工作。我可以注册用户并在http://127.0.0.1:4000/auth上查看他们。
我甚至可以编辑网站上的数据,但我不知道这些数据在哪里,或者是否在我的模拟器上。
firebase.js:

const firebaseConfig = {
  apiKey: process.env.REACT_APP_APIKEY,
  authDomain: process.env.REACT_APP_AUTHDOMAIN,
  projectId: process.env.REACT_APP_PROJECTID,
  storageBucket: process.env.REACT_APP_STORAGEBUCKET,
  messagingSenderId: process.env.REACT_APP_MESSAGINGSENDERID,
  appId: process.env.REACT_APP_APPID,
  measurementId: process.env.REACT_APP_MEASUREMENTID,
};

const hostname = window.location.hostname;
const app =
  hostname === "localhost"
    ? initializeApp({
        apiKey: "demo-key",
        authDomain: "demo-test",
        projectId: "demo-test",
        storageBucket: "default-bucket",
        appId: "demo-appId",
      })
    : initializeApp(firebaseConfig);

export const auth = getAuth(app);
export const db = getFirestore(app);
export const storage = getStorage(app);
export const functions = getFunctions(app);
export const analytics = getAnalytics(app);

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

firebase.json:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ],
      "predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint"]
    }
  ],
  "hosting": {
    "public": "build",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },
  "storage": {
    "rules": "storage.rules"
  },
  "emulators": {
    "auth": {
      "port": 9099,
      "host": "127.0.0.1"
    },
    "functions": {
      "port": 5001,
      "host": "127.0.0.1"
    },
    "firestore": {
      "port": 8080,
      "host": "127.0.0.1"
    },
    "hosting": {
      "port": 5000,
      "host": "127.0.0.1"
    },
    "storage": {
      "port": 9199,
      "host": "127.0.0.1"
    },
    "ui": {
      "enabled": true,
      "port": 4000,
      "host": "127.0.0.1"
    },
    "singleProjectMode": true
  }
}

需要说明的是,在firebase.jsonfirebase.js中使用localhost127.0.0.1没有区别
一个用户注册的图像和firebase正在写入的数据库,我不知道如何访问:

请帮帮忙,我真的很感激你的帮助。我一直在试图弄清楚这一点有一段时间了。

kx1ctssn

kx1ctssn1#

localhost可以解析为不同的环回地址,具体取决于您的系统(IPv4与IPv6)

ping localhost -c 1
PING localhost(localhost (::1)) 56 data bytes
ping 127.0.0.1 -c 1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

这里看起来发生的事情是,您的模拟器正在www.example.com上运行127.0.0.1,但您正在尝试通过connectFirestoreEmulator连接到localhost
解决方法是在那里也使用显式IP地址:
connectFirestoreEmulator(db, "127.0.0.1", 8080);
模拟器套件尚不支持侦听IPv6地址。
编辑:还要更新hostname === "localhost"检查,因为在浏览器中连接到127.0.0.1时,它们将不再解析true

nzk0hqpo

nzk0hqpo2#

从我的评论回答:
为了使模拟器项目ID与应用程序中的项目ID匹配,请使用以下命令启动模拟器:
firebase emulators:start --project=demo-test,其中demo-testinitializeApp配置中的项目ID。

相关问题