最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Argument of type 'import("mongoose").Types.ObjectId' is not assignable to parameter

programmeradmin5浏览0评论

Argument of type 'import("mongoose").Types.ObjectId' is not assignable to parameter of type 'import("mongoose").Schema.Types.ObjectId'. Type 'ObjectId' is missing the following properties from type 'ObjectId': auto, defaultOptions, OptionsConstructor, cast, and 20 more.ts(2345)

this error is showing when i convert an string objectId to to mongoose Objectid;

  const questId  = new mongoose.Types.ObjectId(questionId as string )
      const response = await this.__questionRepository.createNewAnswer(
        answer,
        questId,// here showing the above typescript  error
        userId,
        userType
      );



//this is how i defined the questionId in Schema

 questionId: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Question',
            required: true
        }

Argument of type 'import("mongoose").Types.ObjectId' is not assignable to parameter of type 'import("mongoose").Schema.Types.ObjectId'. Type 'ObjectId' is missing the following properties from type 'ObjectId': auto, defaultOptions, OptionsConstructor, cast, and 20 more.ts(2345)

this error is showing when i convert an string objectId to to mongoose Objectid;

  const questId  = new mongoose.Types.ObjectId(questionId as string )
      const response = await this.__questionRepository.createNewAnswer(
        answer,
        questId,// here showing the above typescript  error
        userId,
        userType
      );



//this is how i defined the questionId in Schema

 questionId: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Question',
            required: true
        }
Share Improve this question edited Jan 20 at 16:27 Aswanth asked Jan 20 at 0:47 AswanthAswanth 12 bronze badges 2
  • Hello, this is because the questionId might be getting null/undefined so try to check whether it is correct or not. – user28234598 Commented Jan 20 at 12:49
  • const questId = new mongoose.Types.ObjectId(questionId as string) as unknown as mongoose.Schema.Types.ObjectId; //in schema questionId: { type: mongoose.Types.ObjectId, ref: 'Question', required: true } i solved this error by making change like this – Aswanth Commented Jan 20 at 16:19
Add a comment  | 

1 Answer 1

Reset to default 0
//atlast i solved this issue by making these changes

const questId = new  mongoose.Types.ObjectId(questionId as string) as unknown as  mongoose.Schema.Types.ObjectId;

//in schema
questionId: {

            type: mongoose.Types.ObjectId,

            ref: 'Question',

            required: true
        }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论