【发布时间】: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