Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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
Javascript 在模式中编辑注释,然后返回注释模式并将其删除时出现意外错误_Javascript_React Native_Firebase Realtime Database - Fatal编程技术网

Javascript 在模式中编辑注释,然后返回注释模式并将其删除时出现意外错误

Javascript 在模式中编辑注释,然后返回注释模式并将其删除时出现意外错误,javascript,react-native,firebase-realtime-database,Javascript,React Native,Firebase Realtime Database,当我打开注释模式并按下编辑按钮时,它会打开另一个模式,然后我编辑注释并按下编辑按钮并关闭编辑模式,然后进行更改并编辑注释,但当我在同一注释上按下删除按钮时,它会给我该错误 TypeError:null不是对象(计算'snapshot.val().commentTxt') 而'snapshot.val().commentTxt'在编辑功能中,而不是在删除功能中,那么为什么只有当我按下删除按钮时才会出现它-知道删除按钮可以工作并删除注释,并且不受错误的影响- 以下是编辑和删除代码: editComm

当我打开注释模式并按下编辑按钮时,它会打开另一个模式,然后我编辑注释并按下编辑按钮并关闭编辑模式,然后进行更改并编辑注释,但当我在同一注释上按下删除按钮时,它会给我该错误

TypeError:null不是对象(计算'snapshot.val().commentTxt')

'snapshot.val().commentTxt'在编辑功能中,而不是在删除功能中,那么为什么只有当我按下删除按钮时才会出现它-知道删除按钮可以工作并删除注释,并且不受错误的影响-

以下是编辑和删除代码:

editComment = () => {
        firebase.database()
        .ref(`posts/${this.props.postKey}/comments/${this.state.editCommentKey}`)
        .on('value', snapshot =>{
            if (this.state.editComment === snapshot.val().commentTxt){
                this.setState({editCommentModalVisible: false})
            }
            else{
                firebase.database()
                .ref(`posts/${this.props.postKey}/comments/${this.state.editCommentKey}/commentTxt`)
                .set(this.state.editComment)
                this.setState({editCommentModalVisible: false})
            }
        })
    }
    deleteComment = (item) => {
        firebase.database().ref('posts').child(this.props.postKey/*'-M0IviCqMGE_PxoqNd0W'*/)
        .on('value', snap => {this.makeCommentIncrement =  snap.val().commentsNumber})
        this.makeCommentIncrement= this.makeCommentIncrement-1
        firebase.database().ref(`posts/${this.props.postKey}/commentsNumber`).set(this.makeCommentIncrement)
        firebase.database().ref(`posts/${this.props.postKey}/comments/${item.commentKey}`).remove()
        .catch(error => {
            alert(error.toString())
            return
        })
    }

在('value',someFunction)上执行
someRef.on时,添加一个侦听器。每次
someRef
处的数据更改时,都会执行函数
someFunction
。这些侦听器必须手动分离(请参阅),或者您可以使用
一次而不是
上的
(请参阅)

删除注释时,会更改注释引用处的数据(将其设置为null),因此
snapshot.val().commentTxt
会给您一个错误,因为
snapshot.val()
为null