[英]FireStore in React-Native
Hi I have been trying to access Firestore from React-native App您好我一直在尝试从 React-native App 访问 Firestore
My Tried Code我尝试过的代码
import firebase from '@react-native-firebase/app'
import firestore from '@react-native-firebase/firestore';
import * as Actions from '../../../StateManager/Actions/ActionTypes'
export function getVersion() {
return async (dispatch) => {
/// Method 1
firestore().collection('Versions').get().then(querySnapshot => {
console.log('Snapshot: ' + querySnapshot)
return null
});
/// Method 2
firestore().collection('Versions').get()
.then(querySnapshot => {
console.log(querySnapshot);
console.log(querySnapshot._docs);
})
}
}
I none worked我没有工作
Configurations Done配置完成
npm install --save @react-native-firebase/app
npm install --save @react-native-firebase/app
npm install —save @react-native-firebase/firestore
in React Appnpm install —save @react-native-firebase/firestore
in React Appcd iOS/ && pod install
it installed all the dependency related to Firebase and Firestore in iOS App cd iOS/ && pod install
它在 iOS App 中安装了与 Firebase 和 Firestore 相关的所有依赖项Debugging Console Info: On Calling My getVersion() below got in debugging mode调试控制台信息:在下面调用我的 getVersion() 时进入调试模式
I have gone through many Question on Stack couldn't get any positive output我经历了很多关于堆栈的问题无法得到任何肯定 output
Included Libraries:包含的库:
React-Navigation, Redux, redux-thunk, Firebase, Firestore
My Store and redux are properly configured and Screen.js (my Root screen where I call this is properly connected to Redux and store)我的商店和 redux 已正确配置和 Screen.js(我称之为的根屏幕已正确连接到 Redux 和商店)
Below way I am calling it下面我称之为
componentDidMount() {
this.props.getVersion()
if (Platform.OS === 'android') {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress)
}
}
FireStore Structure: FireStore 结构:
FireStore Rules: FireStore 规则:
Here I am updating answer based on my R&D在这里,我根据我的研发更新答案
I am able to get version and status from Firestore Collection我可以从 Firestore Collection 获取版本和状态
Updating below may help others who struggles for syntax下面的更新可能会帮助其他语法困难的人
export function getVersion(versionID) {
return async (dispatch) => {
try {
/// For Real Time
firestore().collection('Versions').onSnapshot(querySnapshot => {
querySnapshot.forEach(doc => {
if (doc.exists) {
const { version, status } = doc.data()
}
});
});
/// For One Time
firestore().collection('Versions').get().then(querySnapshot => {
querySnapshot.forEach(doc => {
if (doc.exists) {
const { version, status } = doc.data()
}
});
});
} catch (error) {
console.log("*** Firebase - error setting document", { error });
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.