react-native - 使用 firestore 时出现以下错误错误:FIRESTORE (8.1.2) INTERNAL ASSERTION FAILED: Unexpected state
问题描述
我目前正在开发一个应用程序,它工作了一段时间......当我注册时,我将用户添加到 firestore,它仍然有效,但现在每当我在其他屏幕上使用 firestore 时,它都会抛出以下错误:
@firebase/firestore:, Firestore (8.1.2): FIRESTORE (8.1.2) INTERNAL ASSERTION FAILED: Unexpected state
Stack trace:
node_modules/react-native/Libraries/LogBox/LogBox.js:148:8 in registerError
node_modules/react-native/Libraries/LogBox/LogBox.js:59:8 in errorImpl
node_modules/react-native/Libraries/LogBox/LogBox.js:33:4 in console.error
node_modules/expo/build/environment/react-native-logs.fx.js:27:4 in error
http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:171277:27
这是我遇到错误的屏幕代码
import React from 'react';
import {
View,
Text,
StyleSheet,
TextInput,
ScrollView,
TouchableOpacity,
Modal,
Platform,
ToastAndroid,
Alert,
} from 'react-native';
import DropDownPicker from 'react-native-dropdown-picker';
import { FontAwesome5, FontAwesome, Entypo } from '@expo/vector-icons';
import firebase from 'firebase';
import db from '../Config/Firebase';
import { CheckBox } from 'react-native-elements';
import Constants from 'expo-constants'
export default class Add extends React.Component{
state = {
allVisitsCompleted: false,
patientId: Math.floor(Math.random() * 1000000000) + 1,
patientName: '',
age: '',
address: '',
phoneNumber: '',
mobileNumber: '',
gender: '',
visitDate: '',
date: null,
modalVisible: false,
teethNumberModalVisible: false,
patient: [],
addPressed: false,
patientAdded: false,
};
onAddPatient = async () => {
firebase.firestore().collection("Patients").add({
patientName:this.state.patientName
})
}
render(){
var patientName = this.state.patientName
var patientId = this.state.patientId
return(
<ScrollView style={{ backgroundColor: '#F5FFFA' }}>
<View style={{ marginHorizontal: 19, marginTop: Constants.statusBarHeight}}>
<Text style={styles.header}>Patient name</Text>
<TextInput
placeholder=""
style={{
fontSize: 20,
fontWeight: '400',
height: 25,
borderBottomWidth: StyleSheet.hairlineWidth,
marginTop: 5,
}}
value={this.state.patientName}
onChangeText={(patientName) =>
this.setState({ patientName: patientName })
}
/>
</View>
<View style={{ marginHorizontal: 19, marginTop: 20 }}>
<Text style={styles.header}>Patient ID</Text>
<Text style={{ fontSize: 20 }}>{patientId}</Text>
</View>
<View style={{ marginHorizontal: 19, marginTop: 20 }}>
<Text style={styles.header}>Age</Text>
<TextInput
placeholder=""
style={{
fontSize: 20,
fontWeight: '400',
height: 25,
borderBottomWidth: StyleSheet.hairlineWidth,
marginTop: 5,
}}
value={this.state.age}
onChangeText={(age) => this.setState({ age: age })}
/>
</View>
<View style={{ marginHorizontal: 19, marginTop: 20 }}>
<Text style={styles.header}>Address</Text>
<TextInput
placeholder=""
style={{
fontSize: 20,
fontWeight: '400',
height: 25,
borderBottomWidth: StyleSheet.hairlineWidth,
marginTop: 5,
}}
value={this.state.address}
onChangeText={(address) => this.setState({ address: address })}
/>
</View>
<View style={{ marginHorizontal: 19, marginTop: 20 }}>
<Text style={styles.header}>Mobile number</Text>
<TextInput
placeholder=""
style={{
fontSize: 20,
fontWeight: '400',
height: 25,
borderBottomWidth: StyleSheet.hairlineWidth,
marginTop: 5,
}}
value={this.state.mobileNumber}
onChangeText={(mobileNumber) =>
this.setState({ mobileNumber: mobileNumber })
}
/>
</View>
<View style={{ marginHorizontal: 19, marginTop: 20 }}>
<Text style={styles.header}>Phone number</Text>
<TextInput
placeholder=""
style={{
fontSize: 20,
fontWeight: '400',
height: 25,
borderBottomWidth: StyleSheet.hairlineWidth,
marginTop: 5,
}}
value={this.state.phoneNumber}
onChangeText={(phoneNumber) =>
this.setState({ phoneNumber: phoneNumber })
}
/>
</View>
<View style={{ marginHorizontal: 19, marginTop: 20 }}>
<Text style={styles.header}>Gender</Text>
<TextInput
placeholder=""
style={{
fontSize: 20,
fontWeight: '400',
height: 25,
borderBottomWidth: StyleSheet.hairlineWidth,
marginTop: 5,
}}
value={this.state.gender}
onChangeText={(gender) => this.setState({ gender: gender })}
/>
</View>
<View style={{ marginHorizontal: 19, marginTop: 20 }}>
<Text style={styles.header}>All visits completed</Text>
<CheckBox
checkedColor="#0F0"
checkedTitle="Yes"
onPress={() =>
this.setState({
allVisitsCompleted: !this.state.allVisitsCompleted,
})
}
size={30}
title="No"
uncheckedColor="#F00"
checked={this.state.allVisitsCompleted}
checkedIcon="check"
uncheckedIcon="close"
/>
</View>
<View style={{ marginHorizontal: 25, marginVertical: 20 }}>
<TouchableOpacity
style={{
alignItems: 'center',
backgroundColor: '#62C7A1',
padding: 10,
borderRadius: 20,
}}
onPress={() => {
this.onAddPatient();
this.setState({ addPressed: true });
}}>
{this.state.patientAdded === true ? (
<PacmanIndicator color="#FFF" size={30} />
) : (
<Text
style={{
fontSize: 20,
color: 'white',
fontWeight: 'bold',
textTransform: 'uppercase',
}}>
Add
</Text>
)}
</TouchableOpacity>
</View>
</ScrollView>
)
}
}
const styles = StyleSheet.create({
header: {
fontSize: 13,
fontWeight: '200',
color: '#8e93a1',
textTransform: 'uppercase',
},
chooseDate: {
fontSize: 13,
fontWeight: '600',
color: '#696969',
textTransform: 'uppercase',
},
});
如果您有任何想法如何解决此问题,请告诉我...提前致谢!
解决方案
我相信这可能是一个同步问题,因为您的onAddPatient
函数是异步函数,但您没有等待 firestore 添加文档,这会产生意外状态。要修复它,您只需要等待它,所以:
onAddPatient = async () => {
await firebase.firestore().collection("Patients").add({
patientName:this.state.patientName
})
}
推荐阅读
- javascript - 将子图像附加到表格顶部
- sql - 如何汇总多次访问我的商店的用户数量
- yii2 - Kartik Gridview 切换输入类型
- python - 如果搜索的字符串包含字符串开头的子字符串,Pandas string.contains 将不起作用
- objective-c - 如何在 Mac OS X 10.15.2 (Catalina) 上使用 Obj-C、Xcode 11.3.1 在代码中重新定义应用程序窗口的大小/原点,覆盖 nib/xib 文件参数?
- sql - 如何将用户名和密码从 Excel 传递到 Access DB?
- c - 套接字编程 - 简单的客户端/服务器
- form-recognizer - 在表单识别器中实现反馈循环和重新训练的解决方法
- docker - Tomcat Web 应用程序未接收通过 AWS ELB 发送的请求
- ios - 如何在没有 NavigationLink 的情况下使用 SwiftUI 推送新的根视图?