2017-10-04 167 views
1

Firestore docs只显示了如何设置使用firebase-admin公司的FireStore,这似乎对我来说,基本上是一个服务器端解决方案。即使我们谈论的是使用云功能,这仍然是一个小型的服务器与浏览器和数据库之间的一个额外的抽象层。我正试图弄清楚如何进行类似的设置,直接从浏览器代码访问实时数据库。如何建立云公司的FireStore静态托管网站

在5分钟时为YouTube: Getting Started With Cloud Firestore on the Web | Complete Tutorial由谷歌提供的,我清楚地看到正在使用这种类型的配置,除了现在初始化程序之后,我们称之为firestore()方法,而不是database()方法来获取根引用。

我已经重新启动了启动firestore的项目,并且已经将项目规则设置为允许任何人访问它。但对我来说是不包括在我的火力实例firebase()方法。

const firebase = require('firebase') 

const config = { 
    apiKey: 'long_ugly_string', 
    authDomain: 'my_app_id.firebaseapp.com', 
    databaseURL: 'https://my_app_id.firebaseio.com', 
    projectId: 'my_app_id', 
} 

firebase.initializeApp(config) 

console.log(firebase.firebase) // undefined 
const firestore = firebase.firestore() // TypeError: firebase.firestore is not a function 

我也尝试创建一个100%全新的项目,以确保问题不被旧的设置从实时数据库安装挥之不去所致。但我对这个新项目有同样的问题。我的火力实例结束了一个database()方法,但没有firestore()方法。

+0

您使用的是哪个版本的Firebase? –

+1

的火力文档展示了如何使用几种不同的语言。服务器端JavaScript显示在“Node.js”选项卡下。您应该查看客户端JavaScript的“Web”选项卡。确保您[使用正确的图书馆版本](https://firebase.google.com/docs/firestore/quickstart#set_up_your_development_environment) – Sidney

回答

2

由于从@Sidney发表评论,我可以尝试解决问题。我事实上是在查看node.js选项卡,而不是文档中的web选项卡。缺少的元素是致电require('firebase/firestore'),以使其可用于firebase模块。以下是更正后的设置代码:

const firebase = require('firebase') 
require('firebase/firestore') // *** this line was missing *** 

const config = { 
    apiKey: 'long_ugly_string', 
    authDomain: 'my_app_id.firebaseapp.com', 
    databaseURL: 'https://my_app_id.firebaseio.com', 
    projectId: 'my_app_id', 
} 

firebase.initializeApp(config) 

const firestore = firebase.firestore() 

// rest of code ... 
相关问题