导入部分:
...
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
realtimeDatabase
的值。您可以混淆项目名称,但我想看到其余部分。此外:您是否考虑按照此处所示获取数据库实例:https://firebase.google.com/docs/database/web/start#add_the_js_sdk_and_initialize - Frank van Puffelen