【问题标题】:unexpected error when i edit comment in a modal then return to the comments modal and delete it当我在模式中编辑评论然后返回到评论模式并删除它时出现意外错误
【发布时间】:2020-03-03 13:00:31
【问题描述】:

当我打开 cmets 的模态并按下编辑按钮时,它会打开另一个模态然后我编辑评论并按下编辑按钮并关闭编辑模态并进行更改并编辑评论但是当我按下删除同一评论上的按钮它给了我那个错误

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
        })
    }

【问题讨论】:

    标签: javascript react-native firebase-realtime-database


    【解决方案1】:

    当您执行someRef.on('value', someFunction) 时,您添加了一个侦听器。每次someRef 处的数据发生变化时,都会执行函数someFunction。这些监听器必须手动分离(参见Detach listeners),或者您可以使用once 而不是on(参见Read data once)。

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

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2018-10-28
      相关资源
      最近更新 更多