突然,我的路线开始告诉我这个错误,我不知道它有什么问题。
我正在尝试从中间件传递id。并使用它来获取文档。
我创建了一个字段名hostId,其中存储了在string表单中创建项目的用户的id。
错误:
CastError: Cast to ObjectId failed for value "proj" (type string) at path "_id" for model "Student"
at model.Query.exec (D:\KabirProject\FindAlly\node_modules\mongoose\lib\query.js:4545:21)
at model.Query.Query.then (D:\KabirProject\FindAlly\node_modules\mongoose\lib\query.js:4644:15)
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
messageFormat: undefined,
stringValue: '"proj"',
at ObjectId.cast (D:\KabirProject\FindAlly\node_modules\mongoose\lib\schema\objectid.js:245:12)
at ObjectId.SchemaType.applySetters (D:\KabirProject\FindAlly\node_modules\mongoose\lib\schematype.js:1135:12)
at ObjectId.SchemaType._castForQuery (D:\KabirProject\FindAlly\node_modules\mongoose\lib\schematype.js:1567:15)
at ObjectId.SchemaType.castForQuery (D:\KabirProject\FindAlly\node_modules\mongoose\lib\schematype.js:1557:15)
at ObjectId.SchemaType.castForQueryWrapper (D:\KabirProject\FindAlly\node_modules\mongoose\lib\schematype.js:1534:20)
at cast (D:\KabirProject\FindAlly\node_modules\mongoose\lib\cast.js:336:32)
at model.Query.Query.cast (D:\KabirProject\FindAlly\node_modules\mongoose\lib\query.js:4968:12)
at model.Query.Query._castConditions (D:\KabirProject\FindAlly\node_modules\mongoose\lib\query.js:2056:10)
at model.Query.<anonymous> (D:\KabirProject\FindAlly\node_modules\mongoose\lib\query.js:2335:8)
at model.Query._wrappedThunk [as _findOne] (D:\KabirProject\FindAlly\node_modules\mongoose\lib\helpers\query\wrapThunk.js:27:8)
at D:\KabirProject\FindAlly\node_modules\kareem\index.js:370:33
at processTicksAndRejections (internal/process/task_queues.js:77:11),
valueType: 'string'
这是我的路线:
router.get('/student/projects', signin,(req,res)=>{
try{
const id = req.user._id
const Projects = PersonalProject.find({hostId: id});
const user = req.user;
res.render("student/myProjects", {Projects, user});
} catch(err){
console.log(err);
}
});
签名中间件:
function signin(req, res, next){
const {cookies} = req;
if(!cookies){
return res.status(401).json({error:"You must be signed in"})
}
const token = cookies.jwtToken.replace("Bearer ","")
jwt.verify(token,process.env.ACCESS_TOKEN_SECRET,(error,user)=>{
if(error){
return res.status(401).json({error: "You must be signed in"})
}
req.user = user;
console.log(user._id)
next();
});
}
个人项目模式:
const mongoose = require('mongoose');
const personalProjectSchema = new mongoose.Schema({
title:{
type: String,
required: true,
trim: true,
minlength: [2, "Length of your name is too short"],
maxlength: [50, "Max length reached"]
},
host:{
type: String,
required: true,
minlength: [2, "field length is too short"],
maxlength: [50, "Max length reached"]
},
hostId:{
type:String,
required: true
},
year:{
type: String,
required: true,
trim: true,
minlength: [2, "Length of your name is too short"],
maxlength: [30, "Max length reached"]
},
branch:{
type: String,
required: true,
trim: true,
minlength: [2, "Length of your name is too short"],
maxlength: [50, "Max length reached"]
},
description:{
type:String,
required: true,
trim:true
},
role:{
type: String,
trim: true,
minlength: [2, "Length of your name is too short"],
maxlength: [15, "Max length reached"]
},
status:{
type: String,
default: "ACTIVE",
required: true
},
githubLink:{
type: String,
trim: true
},
teamLimit:{
type: Number,
trim: true,
required: true
},
teammates:[]
},
{timestamps:true}
);
module.exports = mongoose.model('Project', personalProjectSchema);
学生模式:
const mongoose = require('mongoose');
const studentSchema = new mongoose.Schema({
name:{
type: String,
required: true,
trim: true,
minlength: [2, "Length of your name is too short"],
maxlength: [50, "Max length reached"]
},
email:{
type: String,
required: true,
trim: true
},
gender:{
type:String,
required: true,
trim: true,
minlength: [4, "Length of your name is too short"],
maxlength: [6, "Max length reached"]
},
year:{
type: String,
required: true,
trim: true,
minlength: [2, "Length of your name is too short"],
maxlength: [30, "Max length reached"]
},
branch:{
type: String,
required: true,
trim: true,
minlength: [2, "Length of your name is too short"],
maxlength: [50, "Max length reached"]
},
password:{
type: String,
required: true,
},
mobNo:{
type: Number,
required: true,
minlength: [10, "Length of your name is too short"],
maxlength: [10, "Max length reached"]
},
role:{
type: String,
trim: true,
minlength: [2, "Length of your name is too short"],
maxlength: [15, "Max length reached"]
}
},
{timestamps: true}
);
mongoose.model("Student", studentSchema);
请帮我找出问题。
发布于 2021-10-24 20:12:11
首先,您有一个强制转换错误,这意味着您需要使用smth:Schema.Types.ObjectId(_id)
来将您的值转换为ObjectId类型。第二,看起来您的代码的其他部分(不是您提供的代码)有问题,检查在哪里使用聚合与学生模型,并将_id支柱转换为ObjectId类型
https://stackoverflow.com/questions/69698380
复制相似问题