突然我的路由开始显示我这个错误,我不知道有什么问题。
我正试图从中间件传递id。然后用它来获取文档。
我创建了一个字段名称hostId,其中以字符串形式存储创建项目的用户的id。
错误:
CastError: Cast to ObjectId failed for value "proj" (type string) at path "_id" for model "Student"
at model.Query.exec (D:KabirProjectFindAllynode_modulesmongooselibquery.js:4545:21)
at model.Query.Query.then (D:KabirProjectFindAllynode_modulesmongooselibquery.js:4644:15)
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
messageFormat: undefined,
stringValue: '"proj"',
at ObjectId.cast (D:KabirProjectFindAllynode_modulesmongooselibschemaobjectid.js:245:12)
at ObjectId.SchemaType.applySetters (D:KabirProjectFindAllynode_modulesmongooselibschematype.js:1135:12)
at ObjectId.SchemaType._castForQuery (D:KabirProjectFindAllynode_modulesmongooselibschematype.js:1567:15)
at ObjectId.SchemaType.castForQuery (D:KabirProjectFindAllynode_modulesmongooselibschematype.js:1557:15)
at ObjectId.SchemaType.castForQueryWrapper (D:KabirProjectFindAllynode_modulesmongooselibschematype.js:1534:20)
at cast (D:KabirProjectFindAllynode_modulesmongooselibcast.js:336:32)
at model.Query.Query.cast (D:KabirProjectFindAllynode_modulesmongooselibquery.js:4968:12)
at model.Query.Query._castConditions (D:KabirProjectFindAllynode_modulesmongooselibquery.js:2056:10)
at model.Query.<anonymous> (D:KabirProjectFindAllynode_modulesmongooselibquery.js:2335:8)
at model.Query._wrappedThunk [as _findOne] (D:KabirProjectFindAllynode_modulesmongooselibhelpersquerywrapThunk.js:27:8)
at D:KabirProjectFindAllynode_moduleskareemindex.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);
}
});
Signin中间件:
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);
请帮我找出问题所在。
首先,你有一个Cast错误,这意味着你需要像这样使用smith:Schema.Types.ObjectId(_id)
将值转换为ObjectId类型。其次,看起来你在代码的其他部分有问题(不是你提供的),检查你在哪里使用学生模型的聚合,并将_id prop转换为ObjectId类型