Home > front end >  Firebase Web get in realtime database throwing error though path is correct
Firebase Web get in realtime database throwing error though path is correct

Time:08-04

import section:

...
import { ref, getDownloadURL, deleteObject } from "firebase/storage";
import { child, get } from "firebase/database";
...

This is the code:

function getDirectLink() {
    console.log(realtimeDatabase)
    try {
      get(child(ref(realtimeDatabase), `qrCodes/`   currentUser.uid   "/"))
      .then((snapshot) => {
        console.log(snapshot)
        if (snapshot.exists()) {
          var data = snapshot.val();
          if (data["direct_link"]) {
            setDirectLink(data["direct_link"]);
          }
        }
      })
      .catch((error) => {
        console.error(error);
      });
    } catch (error) {
      console.log(error);
    }
  }

This is the error:

TypeError: Cannot read properties of undefined (reading 'pieceNum_')
    at pathGetFront (Path.ts:80:1)
    at child (Reference_impl.ts:543:1)
    at getDirectLink (DatabaseContext.js:584:1)
    at DatabaseContext.js:151:1
    at invokePassiveEffectCreate (react-dom.development.js:23487:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
    at invokeGuardedCallback (react-dom.development.js:4056:1)
    at flushPassiveEffectsImpl (react-dom.development.js:23574:1)
    at unstable_runWithPriority (scheduler.development.js:468:1)

I know realtimeDatabase has been initialised properly. As I am using the same variable in other functions as well (which work without any error).

Also I got this database instance exactly as is suggested in https://firebase.google.com/docs/database/web/start#add_the_js_sdk_and_initialize

console output of realtimeDatabase:

Database {_repoInternal: Repo, app: FirebaseAppImpl, type: 'database', _instanceStarted: true, _rootInternal: ReferenceImpl}
app: FirebaseAppImpl
_automaticDataCollectionEnabled: false
_config: {name: '[DEFAULT]', automaticDataCollectionEnabled: false}
_container: ComponentContainer {name: '[DEFAULT]', providers: Map(21)}
_isDeleted: false
_name: "[DEFAULT]"
_options: {apiKey: 'AIzaSyBwecewfcecfewfjDlbys7RAk', authDomain: 'xxxx.firebaseapp.com', projectId: 'fedc-wdcedcft', storageBucket: 'efverfvrev.appspot.com', messagingSenderId: '******', …}
automaticDataCollectionEnabled: (...)
config: (...)
container: (...)
isDeleted: (...)
name: (...)
options: (...)
[[Prototype]]: Object
type: "database"
_instanceStarted: true
_repoInternal: Repo
appCheckProvider_: AppCheckTokenProvider {appName_: '[DEFAULT]', appCheckProvider: Provider, appCheck: null}
authTokenProvider_: FirebaseAuthTokenProvider {appName_: '[DEFAULT]', firebaseOptions_: {…}, authProvider_: Provider, auth_: AuthInterop}
dataUpdateCount: 8
eventQueue_: EventQueue {eventLists_: Array(0), recursionDepth_: 0}
forceRestClient_: false
infoData_: SnapshotHolder {rootNode_: ChildrenNode}
infoSyncTree_: SyncTree {listenProvider_: {…}, syncPointTree_: ImmutableTree, pendingWriteTree_: {…}, tagToQueryMap: Map(0), queryToTagMap: Map(0)}
interceptServerDataCallback_: null
key: "https://xxxxxxxx-default-rtdb.asia-southeast1.firebasedatabase.app/"
nextWriteId_: 1
onDisconnect_: {value: null, children: Map(0)}
persistentConnection_: PersistentConnection {repoInfo_: RepoInfo, applicationId_: 'wfeerwfcerwfrefre3', onDataUpdate_: ƒ, onConnectStatus_: ƒ, onServerInfoUpdate_: ƒ, …}
repoInfo_: RepoInfo {secure: true, namespace: 'xxxxxxxxxx-default-rtdb', webSocketOnly: undefined, nodeAdmin: false, persistenceKey: '', …}
serverSyncTree_: SyncTree {listenProvider_: {…}, syncPointTree_: ImmutableTree, pendingWriteTree_: {…}, tagToQueryMap: Map(0), queryToTagMap: Map(0)}
server_: PersistentConnection {repoInfo_: RepoInfo, applicationId_: 'edwefdewfdefewrfewrf', onDataUpdate_: ƒ, onConnectStatus_: ƒ, onServerInfoUpdate_: ƒ, …}
statsListener_: null
statsReporter_: StatsReporter {server_: PersistentConnection, statsToReport_: {…}, statsListener_: StatsListener}
stats_: StatsCollection {counters_: {…}}
transactionQueueTree_: Tree {name: '', parent: null, node: {…}}
[[Prototype]]: Object
_rootInternal: ReferenceImpl
_orderByCalled: false
_path: Path {pieces_: Array(0), pieceNum_: 0}
_queryParams: QueryParams {limitSet_: false, startSet_: false, startNameSet_: false, startAfterSet_: false, endSet_: false, …}
_repo: Repo {repoInfo_: RepoInfo, forceRestClient_: false, authTokenProvider_: FirebaseAuthTokenProvider, appCheckProvider_: AppCheckTokenProvider, dataUpdateCount: 8, …}
key: (...)
parent: (...)
ref: (...)
root: (...)
_queryIdentifier: (...)
_queryObject: (...)
[[Prototype]]: QueryImpl
_repo: (...)
_root: (...)
[[Prototype]]: Object

CodePudding user response:

The problem lies in the import section. You tried to use ref from firebase/storage. Whereas, it should be from firebase/database.

Do it like this:

import { child, get, ref as databaseRef } from "firebase/database";

.......

get(child(databaseRef(realtimeDatabase), `qrCodes/`   currentUser.uid   "/"))
  • Related