【问题标题】:How to add user's name as well as Id to users field in a chat?如何在聊天中将用户名和 ID 添加到用户字段?
【发布时间】:2020-11-23 02:12:42
【问题描述】:

我正在创建一个具有聊天功能的 Web 应用程序,用户可以加入聊天。一旦用户加入聊天,我想将用户的 ID 及其姓名添加到聊天模式中的用户字段。到目前为止,我可以添加他们的 ID,但我发现很难添加他们的名字。下面,我附上了我的 Chat mongoose 模型,以及将用户添加到聊天中的路线。另外,我附上了我的用户猫鼬模型。任何帮助是极大的赞赏。谢谢!

聊天模型:

    const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ChatSchema = new Schema({
  title: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  creator: {
    type: Schema.Types.ObjectId,
    ref: 'user'
  },
  users: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: 'user'
      },
      name: {
        type: String,
        required: true
      }
    }
  ],
  code: {
    type: String,
    required: true
  },
  posts: [
    {
      text: {
        type: String,
        required: true
      },
      title: {
        type: String,
        required: true
      },
      date: {
        type: Date,
        default: Date.now
      }
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }

});

module.exports = Chat = mongoose.model('chat', ChatSchema);

将用户添加到聊天的路径:

    // @route  Put api/chats
// @desc   Add a user to a chat
// @access Private
router.put('/', [auth,
  [
    check(
      'code',
      'Please include the code for the chat')
      .not()
      .isEmpty(),
    check(
      'password',
      'Please include the password for the chat'
    ).not()
      .isEmpty()
  ]
],
  async (req, res) => {

    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    try {
      const chat = await Chat.findOne({ code: req.body.code });
      //const user = await User.findOne({ user: req.user.id });

      if (!chat) {
        return res.status(400).json({ msg: 'Invalid Credentials' });
      }

      // Check if the chat has already been joined by the user
      if (chat.users.filter(member => member.user.toString() === req.user.id).length > 0) {
        return res.status(400).json({ msg: 'Chat already joined' });
      }

      //console.log(chat.password);

      const isMatch = await bcrypt.compare(req.body.password, chat.password);

      if (!isMatch) {
        return res.status(400).json({ errors: [{ msg: 'Invalid Credentials' }] });
      }

      const newUser = {
        user: req.user.id,
        text: req.user.name
      }


      chat.users.unshift(newUser);

      await chat.save();

      res.json(chat.users);

    } catch (err) {
      console.error(err.message);
      res.status(500).send('Server Error');
    }
  });

用户模型:

    const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = User = mongoose.model('user', UserSchema);

【问题讨论】:

    标签: node.js reactjs mongodb express mongoose


    【解决方案1】:

    在这部分中,您似乎将用户名分配给 text 属性,我认为它应该是 name 而不是 text

    const newUser = {
        user: req.user.id,
        text: req.user.name
      }
    

    代码应该是:

    const newUser = {
        user: req.user.id,
        name: req.user.name //Property should be name
      }
    

    我希望这行得通!

    【讨论】:

    • 这完全符合预期!我真的很感谢你的帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 2021-06-06
    • 2020-12-04
    • 1970-01-01
    相关资源
    最近更新 更多