node.js - 在模型 \"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 获取所有聊天室列表时,我得到了这个错误
解决方案
推荐阅读
- ios - 如何在没有空间问题的情况下创建各种大小的 collectionView 单元格
- javascript - 在reactjs中上传之前的多重图像预览?
- java - 如何在 Selenium Java 中转换 Katalon 脚本?
- angular - 在子组件中测试发出的值
- python-3.x - 自动从网站下载某些 csv 文件
- karate - com.intuit.karate.exception.KarateException: user.feature:11 - 找不到步骤定义方法匹配:匹配响应
- database - PostgreSQL:PgAdmin 上具有 3 个主键的表的外键
- python-3.x - 在 Python 的 for 循环中未激活 Break
- flutter - 如何在 TextField() Flutter 上设置范围数值
- c# - 在 Castle Windsor 中注册泛型接口的多个实现