【问题标题】:Upgrading firebase from 8 to 9 not resolving data in promiseUpgrading firebase from 8 to 9 not resolving data in promise
【发布时间】:2022-12-19 07:24:27
【问题描述】:

I was using Firebase relatime database and was able to get data with get method and was able to return data using promise. Following is the sn-p:

get(databaseRef: string) {
    return new Promise((resolve, reject) => {
      this.database
        .ref(databaseRef)
        .once('value')
        .then((snapshot) => {
          resolve(snapshot.val());
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

Now we want to upgrade to Firebase version 9 and tried new syntax of firebase 9 to get data of realtime database https://firebase.google.com/docs/database/web/read-and-write#read_data_once_with_get. Following is the sn-p:

get(databaseRef: string) {
    const data;
    return new Promise((resolve) => {
      const result = onValue(ref(this.database, databaseRef), (snapshot) => {
        data = (snapshot.val()) || 'Anonymous';
        resolve(data);
      }, {
        onlyOnce: true
      });
    })
}

But with firebase 9 I am not able to get response from promise.

Any suggestions?

【问题讨论】:

    标签: node.js firebase firebase-realtime-database promise


    【解决方案1】:
    import {get, ref} from firebase/database;
    const async getData = ()=>{
    const reference = ref(yourreferencehere);
    const data = await get(reference);
    return data;
    }
    and then use getData method anywhere you want:
    const data = getData();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-22
      • 2022-12-26
      • 1970-01-01
      • 2022-12-02
      • 2022-11-12
      • 2022-12-01
      • 2019-05-22
      • 2022-12-01
      相关资源
      最近更新 更多