我已经编写了一个socket.io代码,用于实时跟踪错误跟踪工具。但是,如果有人发表评论,则相同的数据将被多次创建。我也得到同样的评论。
请在下面的nodejs中通过socket.io代码查看
socket.on('new-comment', (data)=>{
data['commentId'] = shortid.generate()
eventEmitter.emit('save-comment', data)
}) // end of on new comment
eventEmitter.on('save-comment', (data)=>{
let newComment = new CommentModel({
commentId: data.commentId,
comment: data.comment,
commentedBy: data.commentedBy,
createdOn: time.now(),
issueId: data.issueId
})
newComment.save((err, result)=>{
if(err){
console.log(`Error occured: ${err}`)
} else if(check.isEmpty(result)){
console.log("No Comments to be Saved")
} else {
console.log("Comment saved")
console.log(result)
}
})
这是我的angular套接字服务中的代码,用于发布评论
public postComment: any = (commentDetails) =>{
this.socket.emit('new-comment', commentDetails)
} // end on new-comment
我组件中的代码以处理此功能
public postComment: any = () =>{
if(this.comment){
let userComment = {
comment: this.comment,
commentedBy: this.currentUser.firstName+' '+this.currentUser.lastName,
createdOn: Date.now(),
issueId: this.currentIssueId
}
this.socket.postComment(userComment)
this.currentComments.push(userComment)
this.watchersList.push(Cookie.get('fullName'))
} else {
this.toastr.errorToastr("Cannot post empty comments")
}
} // end post comment
让我知道我要去哪里错了