使用 firestore 时出现以下错误错误:FIRESTORE (8.1.2) INTERNAL ASSERTION FAILED: Unexpected state

Posted

技术标签:

【中文标题】使用 firestore 时出现以下错误错误:FIRESTORE (8.1.2) INTERNAL ASSERTION FAILED: Unexpected state【英文标题】:getting the following error when using firestore Error: FIRESTORE (8.1.2) INTERNAL ASSERTION FAILED: Unexpected state 【发布时间】:2021-06-26 04:02:11 【问题描述】:

我目前正在开发一个应用程序,它工作了一段时间......当我注册时,我将用户添加到 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',
    ,
  );

如果您对如何解决此问题有任何想法,请告诉我...提前致谢!

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

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

以上是关于使用 firestore 时出现以下错误错误:FIRESTORE (8.1.2) INTERNAL ASSERTION FAILED: Unexpected state的主要内容,如果未能解决你的问题,请参考以下文章

Flutter Firestore - 获取存储为字符串的 URL 时出现空安全错误

在 Android Studio Firestore 中获取查询时出现错误

创建 Firestore 文档时出现权限错误

使用 Firestore 获取数据时出现颤振错误

检索数据时出现 Vue/Firestore/Firebase 错误

添加“cloud_firestore”时出现颤振错误