【问题标题】:Why is data from the Firebase realtime database only rendering in the console, but not on the page while using Vue.js?为什么使用 Vue.js 时 Firebase 实时数据库中的数据仅在控制台中呈现,而不在页面上呈现?
【发布时间】:2020-10-24 17:32:35
【问题描述】:

当我尝试渲染存储在 Firebase 实时数据库中的值时,我可以在 console.log 中看到它,但在 Vue 中使用 {{ Exams.name }} 语法时它不会在页面上渲染。当我设置相同的东西但使用 Firestore 时,我能够将它呈现到页面上。

我在我的主仪表板视图中使用此代码:

import firebase from '@/firebase/firebaseConfig.js'
import db from '@/firebase/firebaseConfig.js'
import { database } from '@/firebase/firebaseConfig.js'

export default  {

  data() {
    return {
      checkpointReward: {},
      subscribersGained: {},
      ordersRecevied: {},
      salesBarSession: {},
      supportTracker: {},
      productsOrder: {},
      salesRadar: {},
      totalStudents: {},
      examRef: {},
      exams: {},
      studentName: {},
      students: {},
      testingData: {},
      analyticsData,
      dispatchedOrders: []
  }
},

   // Initialize Firebase
    const examRef = database.ref('exams');
        
      examRef.on('value', gotData, errData);

        function gotData(data) {
          const exams = data.val();
          const studentData = exams.name;
          console.log(exams.name);
        }

        function errData(err) {
          console.lof("Error!");
          console.log(err);
        }

【问题讨论】:

    标签: javascript firebase vue.js firebase-realtime-database vuex


    【解决方案1】:

    我不知道您是否粘贴了没有组件的某些部分的代码,但这部分应该在方法内部并将值分配给 this.desired_state_value,如下所示:

      export default  {
    
          data() {
            return {
             checkpointReward: {},
             subscribersGained: {},
             ordersRecevied: {},
             salesBarSession: {},
             supportTracker: {},
             productsOrder: {},
             salesRadar: {},
             totalStudents: {},
             examRef: {},
    
    
            [...]
         },
      methods: {
         getExams(){
            // Initialize Firebase
            this.examRef = database.ref('exams');
        
            this.examRef.on('value', gotData, errData);
    
            function gotData(data) {
               const exams = data.val();
               const studentData = exams.name;
               console.log(exams.name);
        }
    
          function errData(err) {
             console.lof("Error!");
             console.log(err);
        }
    }
    }
    

    【讨论】:

    • 感谢您的帮助。总的来说,我对 Vue 和 JS 还是很陌生 :)
    • 我暂时能够找到一个解决方法,只是使用 Axios。唯一的问题是,当我更改数据库中的数据时,它似乎无法在前端实时响应。
    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多