Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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 socket.io接收到推送通知时出现问题_Javascript_Node.js_Sockets_Socket.io - Fatal编程技术网

Javascript socket.io接收到推送通知时出现问题

Javascript socket.io接收到推送通知时出现问题,javascript,node.js,sockets,socket.io,Javascript,Node.js,Sockets,Socket.io,在我的nodejs服务器中,我通过socket.io为用户使用通知,而sockets未收到通知,我收到错误: (node:8146) UnhandledPromiseRejectionWarning: TypeError: sessionsMap[userId].push is not iterable (cannot read property Symbol(Symbol.iterator)) at Socket.<anonymous> (/var/www/vhosts/h

在我的nodejs服务器中,我通过socket.io为用户使用通知,而sockets未收到通知,我收到错误:

(node:8146) UnhandledPromiseRejectionWarning: TypeError: sessionsMap[userId].push is not iterable (cannot read property Symbol(Symbol.iterator))
    at Socket.<anonymous> (/var/www/vhosts/httpdocs/server/app.js:201:29)
    at Socket.emit (events.js:314:20)
    at /var/www/vhosts/httpdocs/server/node_modules/socket.io/lib/socket.js:528:12
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
(node:8146) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
哪里会有问题

const sessionsMap = {};
RedisClient.subscribe('notification');
RedisClient.subscribe('notification_a');

RedisClient.on('message', async (channel, message) => {
if(channel === 'notification') {
        const notification = JSON.parse(message);
        const receiverId = sessionsMap[notification.notification.user_id];

        // console.log(notification);

        io.sockets.to(receiverId).emit('newNotification', notification);

    } else if(channel === "notification_a") {
        const notification = JSON.parse(message);

        // console.log(notification);

        if(notification.notification.user_id === "0") {
            io.sockets.emit('newNotification', notification);
        } else {
            const receiverId = sessionsMap[notification.notification.user_id];

            io.sockets.to(receiverId).emit('newNotification', notification);
        }
    }

io.on('connection', (socket) => {

    //We ask the client side to send us the user id
    socket.emit('askForUserId');

    //We accept a response to a request to send us a user id
    socket.on('receivedUserID', async (userId) => {
        //We write down the connection id by key (user id)
        const userObject = sessionsMap[userId];
        sessionsMap[userId] = [];

        sessionsMap[userId].push(...userObject);
        sessionsMap[userId].push(socket.id);
    });