当我开始输入消息时,节点服务器一直关闭

Node server keep shutting down when I start typing message

我正在制作一个聊天应用程序,但是当我写一条消息并按下发送时,我的节点服务器一直给我:

TypeError: Cannot read property 'trim' of undefined

TypeError: Cannot read property 'room' of undefined

然后就死机了,不知道为什么,其他都正常,就是服务器死机了。我已经尝试过自己修复它没有用,没有服务器,你可能会猜到会发生什么。

代码如下:

index.js:


var express = require ('express')

var http = require ('http')

var { addUser, removeUser, getUser, getUsersInRoom } = require ('./user')


var router = require ('./router')

var { callbackify } = require('util')


var PORT = process.env.PORT || 5000


var app = express()

var server = http.createServer(app)

var io = require('socket.io')(server, {
  cors: {
    origin: '*',
  }
});

app.use(router)

io.on('connection', (socket) => {
    socket.on('join', ({ name, room }, callback) =>{
    var { error, user } = addUser({ id: socket.id, name, room })


    if(error) return callback(error)

    socket.emit('message', { user: 'admin', text: `Now, I- Oh, ${user.name}, welcome to room ${user.room},enjoy your time here!` })
    socket.broadcast.to(user.room).emit('message',{user: 'admin', text:`Hey, just coming in to say ${user.name} joined, also, how you guys doin.`})

    socket.join(user.room)

    callback()
  })

  socket.on('sendMessage', (message, callback) => {
    var user = getUser(socket.id)


    io.to(user.room).emit('message', { user: user.name, text: message })

    callback()
  })

    socket.on('disconnect', () => {
        console.log('Aw they left :(');

      });
  });

server.listen(PORT, () => console.log (`This is Index, port ${PORT} secured, lots of unfixable bug`))

user.js(问题所在):

const users = []

const addUser = ({ id, name, room }) =>{
  name = name.trim().toLowerCase()
  room = room.trim().toLowerCase()

  const existingUser = users.find((user) => user.room === room && user.name === name)

  if(existingUser){
      return {error: 'Username is already taken you dumbass'}
  }

  const user = {id, name, room}

  users.push(user);

  return{ user }
}
const removeUser = (id) =>{
  const index = users.findIndex ((user) => user.id === id)

if(index !== -1) {
    return users.splice(index, 1)[0]
}
}

const getUser = (id) => users.find((user) => user.id === id)

const getUsersInRoom = (room) => users.filter((user) => user.room === room)

module.exports = { addUser, removeUser, getUser, getUsersInRoom }

router.js:

const express = require('express')
const router = express.Router();

router.get('/*', (req,res) =>{
    res.send(`Server is up and running`)
})

module.exports = router;

这是所有服务器文件,因为问题出在服务器,而不是客户端(这里没有足够的字数,所以我必须这样做) 提前致谢!

这两个错误都是由于应用程序正在尝试访问未定义值的 subfunction/subparameter。

您确定客户端在发出“加入”时发送了正确的参数吗? 你可以在服务器端做一些验证并检查它是否未定义并设置默认值。

name = name ? name.trim().toLowerCase() : "default_name";
room = room ? room.trim().toLowerCase() : "default_room";

至于未定义错误的房间极有可能是由于这部分应用

const existingUser = users.find((user) => user.room === room && user.name === name)

尝试检查是否 users.length,如果它是 0 则不要费心执行 .find(),或者您可以在 .find() 函数中添加验证以检查用户是否未定义,然后 return假;

const existingUser = users.find((user) => user && user.room === room && user.name === name)

socket.on('join', ({ name, room }, callback) =>{

你从哪里得到名字和房间的?