【问题标题】:Vue/Firestore/Firebase error on retrieve data检索数据时出现 Vue/Firestore/Firebase 错误
【发布时间】:2018-11-16 11:03:02
【问题描述】:

我正在使用 Firestore 数据库创建一个 Vue JS 应用程序,但在 Firestore 导入中的某处出现问题(可能)。 它是一个简单的应用程序,只是存储了一些想要显示(最初)以测试其工作的员工详细信息。 (它没有!)它只是使用“firebase”:“^5.0.4”,而不是 vue-firebase 或其他插件。

它的 Firestore 不是 Firebase 实时数据库。

所以在 firebaseInit.js 配置文件中是所有基本配置选项,如下所示

import * as firebase from 'firebase'
// Initialize Firebase
var config = {
  apiKey: "AIzaSyCyKS3QxqtR9HvetpT2vWKFNxa_yeRKdhA",
    authDomain: "vuefsprod-fc778.firebaseapp.com",
    databaseURL: "https://vuefsprod-fc778.firebaseio.com",
    projectId: "vuefsprod-fc778",
    storageBucket: "vuefsprod-fc778.appspot.com",
    messagingSenderId: "1048509841840"
}
firebase.initializeApp(config)

var auth = firebase.auth()
var db = firebase.database()

export function signOut (callback) {
  auth.signOut().then(value => {
    callback()
  }, err => { callback(err) })
}
export default 'firebase/firestore'

然后测试它的脚本sn-p如下(在Helloworld.vue中)

import db from '../firebaseInit'
  export default {
    name: 'home',
    data () {
      return {
        employees: [],
        loading: true
        }
      },
      created() {
       db.collections('employees').get().then(querySnapshot => {
       querySnapshot.forEach(doc => {
       console.log(doc)
       const data = {

       }
       })
       })
      }
    }

Yarn 编译显示的应用程序,但控制台出现如下警告错误

[Vue warn]: Error in created hook: "TypeError: __WEBPACK_IMPORTED_MODULE_0__firebaseInit__.a.collections is not a function"

found in

---> <Home> at src/components/HelloWorld.vue

并且没有数据显示到控制台,员工集合中有 6 项。

我也想知道 a.collections 中的“a”是从哪里来的。

任何关于此的提示或更好的方法,例如使用 vue-firebase 或其他方法,都非常受欢迎。截图如下。

非常感谢

【问题讨论】:

    标签: firebase vue.js google-cloud-firestore


    【解决方案1】:

    问题在于如何从 firebase db 中检索数据

    db.collections('employees')
    

    请更新为

    db.collection('employees')
    

    这应该可以解决这个问题。

    【讨论】:

      【解决方案2】:

      您正在使用实时数据库 (RTDB) 服务而不是 Firestore 服务声明数据库:

      var db = firebase.database()  // <- RTDB
      

      您应该改为:

      var db = firebase.firestore()
      

      由于 RTDB 没有集合,您会收到错误“集合不是函数”

      仅供参考,此处记录了不同的可用服务:https://firebase.google.com/docs/web/setup#use_firebase_services,以及如何访问/声明它们。

      【讨论】:

      • 嗨,试过了,但不高兴,我仍然得到完全相同的错误?
      • 你好@minimallinux 你终于解决问题了吗?你觉得你能接受这个答案吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多