Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 使用firestore时出现以下错误错误:firestore(8.1.2)内部断言失败:意外状态_React Native_Google Cloud Firestore_Expo - Fatal编程技术网

React native 使用firestore时出现以下错误错误:firestore(8.1.2)内部断言失败:意外状态

React native 使用firestore时出现以下错误错误:firestore(8.1.2)内部断言失败:意外状态,react-native,google-cloud-firestore,expo,React Native,Google Cloud Firestore,Expo,我目前正在开发一个应用程序,它在一段时间内运行良好……当我注册时,我将用户添加到firestore,它仍然有效,但现在每当我在其他屏幕上使用firestore时,它总是抛出以下错误: @firebase/firestore:, Firestore (8.1.2): FIRESTORE (8.1.2) INTERNAL ASSERTION FAILED: Unexpected state Stack trace: node_modules/react-native/Libraries/Lo

我目前正在开发一个应用程序,它在一段时间内运行良好……当我注册时,我将用户添加到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',
    },
  });

从“React”导入React;
进口{
看法
文本,
样式表,
文本输入,
滚动视图,
可触摸不透明度,
情态动词
平台,
蟾蜍,
警觉的,
}从“反应本机”;
从“反应本机下拉选择器”导入下拉选择器;
从“@expo/vector icons”导入{FontAwesome5,FontAwesome5,Entypo};
从“firebase”导入firebase;
从“../Config/Firebase”导入数据库;
从“react native elements”导入{CheckBox};
从“expo常量”导入常量
导出默认类Add扩展React.Component{
状态={
AllVisitCompleted:错误,
patientId:Math.floor(Math.random()*100000000)+1,
患者姓名:'',
年龄:'',
地址:'',
电话号码:“”,
移动枚举器:“”,
性别:'',
访问日期:'',
日期:空,
modalVisible:错误,
可见的齿数:false,
病人:[],
答:错,
耐心的:错,
};
onAddPatient=async()=>{
firebase.firestore()集合(“患者”)。添加({
patientName:this.state.patientName
})
}
render(){
var patientName=this.state.patientName
var patientId=this.state.patientId
返回(
患者姓名
this.setState({patientName:patientName})
}
/>
患者ID
{patientId}
年龄
this.setState({age:age})
/>
地址
this.setState({address:address})
/>
手机号码
this.setState({mobileNumber:mobileNumber})
}
/>
电话号码
this.setState({phoneNumber:phoneNumber})
}
/>
性别
this.setState({gender:gender})
/>
所有访问均已完成
这是我的国家({
allVisitsCompleted:!this.state.allVisitsCompleted,
})
}
大小={30}
title=“否”
取消选中颜色=“#F00”
选中={this.state.allvisitcompleted}
checkedIcon=“检查”
取消选中dicon=“关闭”
/>
{
这个病人();
this.setState({addPressed:true});
}}>
{this.state.patientAdded==真(
) : (
添加
)}
)
}
}
const styles=StyleSheet.create({
标题:{
尺寸:13,
fontWeight:'200',
颜色:“#8e93a1”,
textTransform:'大写',
},
选择镇静:{
尺寸:13
重量:'600',
颜色:'#696969',
textTransform:'大写',
},
});

如果你有任何想法如何解决这个问题,请让我知道…提前谢谢

我认为这可能是一个同步性问题,因为您的
onAddPatient
函数是一个异步函数,但您没有等待firestore添加文档,这会生成意外状态。要修复它,您只需等待它,因此:

onAddPatient = async () => {
    await firebase.firestore().collection("Patients").add({
        patientName:this.state.patientName
    })
}

嘿你给出的解决方案对我有效,但现在我在其他屏幕上得到了相同的错误。我个人认为这是因为onSnapshot函数…我会将该代码添加到问题中…但是非常感谢@Vibha我看到这是你在stack上的第一篇文章,所以首先,欢迎你。我建议您将此作为一个单独的问题发布,因为现在我提供的答案(您提到的答案是正确的)不再有效,因为您更改了问题的代码,如果社区中的其他人遇到同样的问题,他们将无法看到如何应用解决方案。如果你想用更多的代码做一个后续问题,你可以做(尽管最好是单独发布一个问题),但是,你必须保留原来的代码。另外,你可以阅读这个,当某人回答你的问题时该怎么做。非常感谢拉斐尔ReMOSO,如果你解决了你所面临的问题,请考虑接受/投票。