从服务器端生成的Firestore模拟器获取客户端React App

时间:2020-09-15 18:03:09

标签: firebase google-cloud-firestore firebase-cli

我目前有一个带有Firebase初始化的React App。我正在使用如下所示的实用程序文件来初始化我的react应用程序:

import * as firebase from 'firebase/app'
import 'firebase/firestore'
import 'firebase/storage'

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
  databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
  appId: process.env.REACT_APP_FIREBASE_APP_ID
}

firebase.initializeApp(firebaseConfig)

if (window.location.hostname === 'localhost') {
  console.log('testing locally -- hitting local firestore emulators')
  firebase.firestore().settings({
    host: 'localhost:8080',
    ssl: false
  })
}

export const db = firebase.firestore()
export const storage = firebase.storage()
export default firebase

如您在上面的代码中看到的,我告诉客户端使用localhost:8080(运行Firestore模拟器的端口)而不是生产数据库。当我从服务器端运行模拟器时,firestore模拟器成功在端口8080上启动。问题是,在客户端上,当尝试从服务器端生成的模拟器获取时,我收到以下错误:

FirebaseError: 
false for 'list' @ L5
    at new n (http://localhost:3000/static/js/1.chunk.js:1381:19)
    at http://localhost:3000/static/js/1.chunk.js:12934:18
    at http://localhost:3000/static/js/1.chunk.js:12935:10
    at n.onMessage (http://localhost:3000/static/js/1.chunk.js:12983:6)
    at http://localhost:3000/static/js/1.chunk.js:12846:18
    at http://localhost:3000/static/js/1.chunk.js:12884:29
    at http://localhost:3000/static/js/1.chunk.js:7563:25

这是权限问题吗?如果是这样,有人解决方案吗?预先感谢

1 个答案:

答案 0 :(得分:1)

是的,看来是安全规则阻止了您访问数据。
为了确保您可以尝试允许访问所有内容,请在firestore.rules中:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

要了解有关安全规则的更多信息:https://firebase.google.com/docs/firestore/security/get-started