【问题标题】:How to access firebase realtimedb in NUXT js如何在 NUXT js 中访问 firebase realtimedb
【发布时间】:2021-11-23 23:49:10
【问题描述】:

我们在 firebase 中有一个 realtimedb,其数据如下:

在我们的 nuxt 页面上,我们有以下方法:

async getDb () {
      const messageRef = this.$firebase.database.ref('test')
      try {
        const snapshot = await messageRef.once('value')
        alert(snapshot.val().message)
      } catch (e) {
        alert(e)
      }
    },

这是由 this 运行的,如下所示:

mounted () {
    this.getDb()
  },

我不断收到此错误:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'database')

我做错了吗?我在 Nuxt.config.js 中有所有的 firebase 配置,并且 suth 工作正常。我还将 realtimedb 添加到服务中:

 services: {
  realtimeDb: true,
  auth: {
    ssr: true,
    // it is recommended to configure either a mutation or action but you can set both
    initialize: {
      onAuthStateChangedMutation: 'ON_AUTH_STATE_CHANGED_MUTATION'
      // onAuthStateChangedAction: 'onAuthStateChangedAction'
    }
  },

您如何实际访问 realtimedb 中的数据,以便在我的页面上显示测试数组?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions nuxt.js


    【解决方案1】:

    您正在使用$firebase,它不包含在此documentation 中。要授予对您的实时数据库的访问权限,请使用以下命令:

    $fire.database
    

    或

    $fireModule.database
    

    注意:

    如果您在 nuxt.config.js 的服务中使用 nuxt/firebase 版本 6,请使用 realtimeDb 和 database 用于 版本 7 strong>,请参阅link 了解更多详情。

    【讨论】:

    • @Joaquin684 让我知道您是否对我的回答进行了澄清,如果您认为我的回答有帮助,您可以投票或接受我的回答。
    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 2019-04-17
    • 2021-08-02
    • 2021-06-27
    • 2018-12-19
    • 2019-01-02
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多