Search code examples
javascriptnode.jsmongoose

ObjectId can't be find on undefined in case when import mongoose


if I import mongoose

import mongoose, { Types } from 'mongoose';

and try to create objected

const fact = new Types.ObjectId(req.u.fact);

It appears error that Types is undefined

so I have to use:

const fact = req.u.fact as Types.ObjectId;

If I remove import mongoose:

import { Types } from 'mongoose';

everything working correct.

Do you understand how to fix it?


Solution

  • I'm not sure is it rigth or not, but now I use 'as'

    const fact = req.u.fact as mongoose.Types.ObjectId;