首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CastError:对于“学生”模型路径"_id“处的值"proj”(类型字符串),强制转换为ObjectId失败

CastError:对于“学生”模型路径"_id“处的值"proj”(类型字符串),强制转换为ObjectId失败
EN

Stack Overflow用户
提问于 2021-10-24 15:50:03
回答 1查看 510关注 0票数 0

突然,我的路线开始告诉我这个错误,我不知道它有什么问题。

我正在尝试从中间件传递id。并使用它来获取文档。

我创建了一个字段名hostId,其中存储了在string表单中创建项目的用户的id。

错误:

代码语言:javascript
运行
复制
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'

这是我的路线:

代码语言:javascript
运行
复制
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);
    }
});

签名中间件:

代码语言:javascript
运行
复制
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();
    });
}

个人项目模式:

代码语言:javascript
运行
复制
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);

学生模式:

代码语言:javascript
运行
复制
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);

请帮我找出问题。

EN

Stack Overflow用户

发布于 2021-10-24 20:12:11

首先,您有一个强制转换错误,这意味着您需要使用smth:Schema.Types.ObjectId(_id)来将您的值转换为ObjectId类型。第二,看起来您的代码的其他部分(不是您提供的代码)有问题,检查在哪里使用聚合与学生模型,并将_id支柱转换为ObjectId类型

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69698380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档