gpt4 book ai didi

javascript - 尽管路径正确,但 Firebase Web 进入实时数据库抛出错误

转载 作者:行者123 更新时间:2023-12-05 04:25:33 36 4
gpt4 key购买 nike

导入部分:

...
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 - 尽管路径正确,但 Firebase Web 进入实时数据库抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73212023/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com