首页 > 解决方案 > 在模型 \"user\"" 的路径 \"_id\" 处获取值 \"{ _id: 'allchatroom' }\" (type Object) 在 mongoose Cast to ObjectId 中的错误失败

问题描述

module.exports.allchatrooms = async(req, res) => {
  try {
    const details = await Chatroom.find({});
    return res.status(200).send({
      status: 200,
      message: "all the chatroom",
      data:details,
    });
  }
  catch (error) {
    return res.status(400).send({
      status: 00,
      err: error,
    });
  }
};
error in postman

{
    "status": 400,
    "success": false,
    "err": {
        "stringValue": "\"{ _id: 'allchatroom' }\"",
        "valueType": "Object",
        "kind": "ObjectId",
        "value": {
            "_id": "allchatroom"
        },
        "path": "_id",
        "reason": {},
        "name": "CastError",
        "message": "Cast to ObjectId failed for value \"{ _id: 'allchatroom' }\" (type Object) at path \"_id\" for model \"user\""
    }
}
model file 
const mongoose = require('mongoose');

const Chatroom =  new mongoose.Schema({
    chatroom_name:{
        type:String,
        require:true
    }

 })
 
module.exports =  mongoose.model('chat',Chatroom);  

我正在尝试为练习制作聊天室应用程序,因为我是这个领域的新手,所以尝试制作这个项目如何处理实时数据但是当我点击 api 获取所有聊天室列表时,我得到了这个错误

标签: node.jsmongodbapipostman

解决方案


推荐阅读