导入部分:

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

代码是这样的:

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);
    }
  }

这是错误:

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)

我知道realtimeDatabase已经正确地初始化了.因为我在其他函数中也使用了相同的变量(它们的工作没有任何错误).

此外,我还获得了这个数据库实例,完全按照https://firebase.google.com/docs/database/web/start#add_the_js_sdk_and_initialize中的建议

控制台输出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

推荐答案

问题出在进口部分.您试图使用firebase/storage中的ref.然而,它应该是从firebase/database开始.

这样做:

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

.......

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

Javascript相关问答推荐

你怎么看啦啦队的回应?

在Java中寻找三次Bezier曲线上的点及其Angular

扩展类型的联合被解析为基类型

使用auth.js保护API路由的Next.JS,FETCH()不起作用

expo 联系人:如果联系人的状态被拒绝,则请求访问联系人的权限

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

在表单集中保存更改时删除';禁用';

FileReader()不能处理Firefox和GiB文件

Cherrio JS返回父div的所有图像SRC

每隔3个项目交替显示,然后每1个项目交替显示

通过解构/功能组件接收props-prop验证中缺少错误"

我在哪里添加过滤器值到这个函数?

我如何才能获得价值观察家&对象&S的价值?

在openstreemap/leaflet中,当鼠标在 map 上移动时,如何在小工具提示中实时显示坐标

我怎么才能拿到幼风登录?

Oracle APEX-如何调用Java脚本函数

Mongoose DeleteOne()和DeleteMany()不工作

Chart.js根据 Select 显示数据

如何有条件地返回Java脚本中的函数?

如何在Java脚本中 Select 该元素?-Chrome扩展