socket.io和eventemiiter创建多个记录

时间:2019-05-22 07:27:51

标签: node.js socket.io eventemitter

我正在为我的项目构建待办事项列表应用程序。我正在使用socket.io,node.js作为后端。我有Todo模块,还有朋友模块

在“朋友”模块中。我希望用户能够从客户端发送朋友请求(发出socket.io事件)。根据请求,socket.io将侦听该事件并发出eventemitter事件,该事件会将数据保存到数据库并向用户发送通知。


        socket.on('send-friend-request', (data)=>{
            setTimeout(()=>{
                eventEmitter.emit('send-request', data)
            }, 2000)
        }) // end socket on send friend request

        eventEmitter.on('send-request', (data)=>{
            let newFriendRequest = {
                friendId: data.friendId,
                friendName: data.friendName,
                email: data.email,
                mobileNumber: data.mobileNumber,
                requestStatus: 'pending',
                modifiedOn: time.now()
            }

            UserModel.findOne({'friends.friendId': socket.userId}, (err, result)=>{
                if(err){
                    logger.error(err, 'socketLib: evenetEmitter - send-request', 6)
                }else if(check.isEmpty(result)){
                    let options = {
                        $push: {
                            friends:{
                                $each: [newFriendRequest],
                                $sort: {modifiedOn: -1}
                            }
                        }
                    }

                    UserModel.findOneAndUpdate({userId: socket.userId}, options)
                        .select('-friends')
                        .exec((err, result)=>{
                        if(err){
                            console.log(err)
                        } else {

                            result.friendId = data.friendId

                            console.log('Friend Request Send')
                            let notify = new NotificationModel({
                                notificationId: shortid.generate(),
                                senderId: socket.userId,
                                receiverId: data.friendId,
                                message: `You Have Received A New Friend Request`,
                                notifiedOn: time.now()
                            })

                            socket.emit(data.friendId, notify)

                            setTimeout(()=>{
                                eventEmitter.emit('recieve-request', result)
                            }, 4000)

                            setTimeout(()=>{
                                notify.save((err, newNotification)=>{
                                    if(err){
                                        console.log(err)
                                    } else {
                                        console.log("New notification Saved")
                                    }
                                })
                            }, 5000)
                        }
                    })
                }else {
                    logger.error("User is already a friend", "socketLib: evenetEmitter - send-request", 6)
                }
            })

        }) // end eventemitter on send-request

eventEmitter.on('recieve-request', (user)=>{
            let receievedRequest = {
                friendId: user.userId,
                friendName: user.firstName+' '+user.lastName,
                email: user.email,
                mobileNumber: user.mobileNumber,
                requestStatus: 'pending',
                modifiedOn: time.now()
            }

            let options = {
                $push: {
                    friends: {
                        $each: [receievedRequest],
                        $sort: {modifiedOn: -1}
                    }
                }
            }

            UserModel.updateOne({userId: user.friendId}, options, {multi: true}, (err, result)=>{
                if(err){
                    console.log(err)
                } else {
                    console.log("Friend Request Received")
                }
            })
        }) // end eventemitter on recieve-request

但是,此事件被多次调用,并且在数据库中创建了多个数据。而且还会向客户端发送多个通知。

请参阅下面的控制台输出

Friend Request Send
Friend Request Send
Friend Request Received
Friend Request Received
Friend Request Received
Friend Request Received
New notification Saved
New notification Saved

该如何纠正?这样就不会多次调用该事件,并避免创建多个数据

0 个答案:

没有答案