Mongoose 获取后验证失败:postedby:Cast to ObjectId失败,因为路径“处的值”;postedby";

Mongoose 获取后验证失败:postedby:Cast to ObjectId失败,因为路径“处的值”;postedby";,mongoose,jwt,Mongoose,Jwt,我在尝试在express Nodejs中插入帖子时遇到此错误转换为ObjectId失败,因为值。此postedby:req.id导致问题。我尝试在postedby字段的Post模型中更改为键入String,但我得到一个错误,表示转换为String失败,以下是模型、路由和JWT代码: //Post模型 const mongoose=require('mongoose'); const{ObjectID}=mongoose.Schema.Types; const postSchema=新的mongo

我在尝试在express Nodejs中插入帖子时遇到此错误
转换为ObjectId失败,因为值
。此
postedby:req.id
导致问题。我尝试在
postedby
字段的
Post
模型中更改为键入
String
,但我得到一个错误,表示转换为String失败,以下是模型、路由和JWT代码:

//Post模型
const mongoose=require('mongoose');
const{ObjectID}=mongoose.Schema.Types;
const postSchema=新的mongoose.Schema({
标题:{
类型:字符串,
必填:“需要标题”,
最小长度:4,
最大长度:250
},
邮寄人:{
类型:ObjectID,ref:“用户”
},
日期:{
类型:日期,
默认值:Date.now
},    
},
{时间戳:true});
module.exports=mongoose.model(“Post”,postSchema);
//JWT
const jwt=require('jsonwebtoken');
函数JWTAuthenticatToken(请求、恢复、下一步){
const{authorization}=req.headers
如果(!授权){
返回res.sendStatus(401).json({错误:“您必须登录!”)
}
const token=authorization.replace('Bearer','')
jwt.verify(token,process.env.jwt\u token\u SECRET,(err,userData)=>{
if(err)返回res.sendStatus(401).json({error:“您必须登录!”)
req.id=用户数据
下一个()
}) 
}
module.exports=JWTAuthenticatToken
//发布路由
const express=require('express');
const router=express.router();
const Post=require('../models/Post');
const JWTAuthenticatToken=require(“../middleware/JWTAuthentication”);
router.post('/post',JWTAuthenticatToken,async(req,res)=>{
const{title,body}=req.body
控制台日志(请求id)
const post=新职位({
标题:标题,,
身体:身体,,
postedby:req.id})
const postresult=wait post.save()
res.json({post:postresult})
});

非常感谢您的帮助。再次感谢您

Does
console.log(req.\u id)
提供了一个有效的参数。ThanksI在上述代码中犯了打字错误。它应该是console.log(req.id),而不是console.log(req.\u id)。谢谢