【发布时间】:2023-01-13 18:41:59
【问题描述】:
我有一个简单的 react-native 应用程序,它已经可以进行身份验证。问题是,我无法读取/写入我的 firebase 实时数据库。
即使数据库中有数据,once and on 方法也不会执行任何回调(成功或失败)。但是,如果我执行设置操作,on/once 方法会返回值。这些值都没有反映在在线数据库中,当我删除应用程序数据时,这些值也不会保留。意思是,Realtime DB 只在本地工作,不反映服务器端。
笔记:
- 实时数据库在 us-central1
- 规则设置为真的为了读和写
-
await reference.once('value')也没有用(永远不会越过这条线) - 能够获得正确的引用对象,因为 google-services.json 包含 firebase url(见代码)
- 我有 firebase 身份验证,所以我确信配置步骤是正确的
- 已重建应用程序并在任何与 Android 相关的更改后运行 gradle build
应用程序.js:
import database from '@react-native-firebase/database';
const App = () => {
useEffect(() => {
const reference = database().ref('test');
console.log(reference); // LOGS CORRECT ADDRESS
reference.once( // NO VALUE RETURNED
'value',
(snapshot) => {
console.log('User data: ', snapshot.val());
},
(error) => {
console.log('error reading messages', error);
}
);
}, []);
return (
<IconComponentProvider IconComponent={MaterialCommunityIcons}>
{/* <AppProvider>
<NavigationStack />
</AppProvider> */}
</IconComponentProvider>
);
};
实时数据库值:
{
"test": "test" // exported from parent level
}
实时数据库规则:
{
"rules": {
".read": true,
".write": true,
}
}
依赖项:
"dependencies": {
"@react-native-firebase/app": "^15.6.0",
"@react-native-firebase/auth": "^15.6.0",
"@react-native-firebase/database": "^15.6.0",
"@react-native-material/core": "^1.3.7",
"@react-navigation/bottom-tabs": "^6.3.3",
"@react-navigation/drawer": "^6.4.4",
"@react-navigation/native": "^6.0.12",
"@react-navigation/native-stack": "^6.8.0",
"eslint": "^7.32.0 || ^8.2.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-react-native": "^4.0.0",
"prettier": "^2.7.1",
"react": "18.0.0",
"react-native": "0.69.5",
"react-native-gesture-handler": "^2.6.0",
"react-native-reanimated": "^2.10.0",
"react-native-safe-area-context": "^4.3.3",
"react-native-screens": "^3.17.0",
"react-native-vector-icons": "^9.2.0",
"yarn": "^1.22.19"
},
添加了 app/build.gradle 依赖项:
implementation platform('com.google.firebase:firebase-bom:30.4.1')
implementation "com.google.firebase:firebase-core"
implementation "com.google.firebase:firebase-auth"
implementation "com.google.firebase:firebase-database"
应用权限:
<uses-permission android:name="android.permission.INTERNET" />
【问题讨论】:
标签: android firebase react-native firebase-realtime-database react-native-firebase