我的应用无法连接到 Firebase 实时数据库。
当我运行下面的代码时,总是会打印日志“未连接”。此外,代码 ref.once('value', snapshot => ...)
不返回任何结果,也不返回错误(永远挂断)。
请告诉我我的代码有什么问题,以及如何解决它。谢谢。
const firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
const config = {
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "..."
};
firebase.initializeApp(config);
function test() {
//Test connnection (https://firebase.google.com/docs/database/web/offline-capabilities#section-connection-state)
const connectedRef = firebase.database().ref('.info/connected');
connectedRef.on('value', (snap) => {
if (snap.val() === true) {
console.log('connected');
} else {
console.error('not connected');
}
});
//Test retrieve data.
const ref = firebase.database().ref('/');
ref.once('value', snapshot => {
const data = snapshot.val();
console.log(data);
}).catch(error => {
console.error(error);
})
}
test();
的重复
答案 0 :(得分:1)
该问题可能与您的防火墙设置或认证问题有关。
例如,如果您的计算机使用自签名证书,Node.js 可能会抛出异常 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>muslim_2b</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(FLUTTER_BUILD_NAME)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
</dict>
</plist>
(令人惊讶的是 Firebase 客户端会默默地忽略它而不抛出任何异常,因此 try/catch 将不起作用)。
您可以通过将 self signed certificate in certificate chain
添加到项目的第一行代码来检查它(请注意,它仅用于测试,因为如果在生产中使用该代码可能会导致安全问题)。