如何为静态托管网站

时间:2017-10-04 21:31:41

标签: javascript firebase browser google-cloud-firestore

Firestore docs仅显示如何使用firebase-admin设置Firestore,这在我看来基本上就像是服务器端解决方案。即使我们正在谈论使用云功能,它仍然是一个迷你服务器,在浏览器和数据库之间有一个额外的抽象层。我正在试图弄清楚如何进行类似的设置,就像直接从浏览器代码访问实时数据库一样。

在Google提供的YouTube: Getting Started With Cloud Firestore on the Web | Complete Tutorial的5分钟后,我看到正在使用此类配置,但现在在初始化应用后,我们调用firestore()方法而不是database()方法获取根参考。

我在启用firestore的情况下重新初始化了项目,并且当前设置的项目规则允许任何人访问它。但对我来说,firebase()方法不包含在我的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%全新的项目,以确保问题不是由实时数据库安装中遗留的旧设置引起的。但我对新项目也有同样的问题。我的firebase实例以database()方法结束,但没有firestore()方法。

1 个答案:

答案 0 :(得分:3)

感谢@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 ...