Skip to content

Upgrade mongoose from 5 to 6 Value of type 'typeof ObjectId' is not callable. Did you mean to include 'new'? #10960

Closed
@zbarakzai

Description

@zbarakzai

version 6.0.12 mongoose

Reporting a bug

getting a type error when calling mongoose.Types.ObjectId()

error Value of type 'typeof ObjectId' is not callable. Did you mean to include 'new'?

What is the expected behavior?

should be able to call mongoose.Types.ObjectId() without adding new keyword.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

version 6.0.12 mongoose
version 14.6.0 node
version 4.1.4 mongodb

the fault lies in any file calling the mongoose.Types.ObjectId()

I recently upgraded mongodb from 3.5.4 to 4.1.4 and mongoose from 5.12.10 to 6.0.12 as I am using typescript 4.4.4 and ts-node 10.4.0 in my project I get a type error Value of type 'typeof ObjectId' is not callable. Did you mean to include 'new'? when using this line of code mongoose.Types.ObjectId() I uninstalled and removed the @types/mongoose from the project as the new version mongoose has its own type and removed globally also but still I get the same error.

Despite the fact that this issue #9608 was tagged as a bug and later tagged as resolved, I continue to receive this error.

Activity

root-io

root-io commented on Nov 6, 2021

@root-io

Can you try this:

import { Schema } from "mongoose";

foo: { type: Schema.Types.ObjectId }
zbarakzai

zbarakzai commented on Nov 6, 2021

@zbarakzai
Author

I don't need it as a type my intention is to create an ObjectId instance like this _id: variant._id ? variant._id : mongoose.Types.ObjectId(),

IslandRhythms

IslandRhythms commented on Nov 8, 2021

@IslandRhythms
Collaborator

possible that this https://mongoosejs.com/docs/migrating_to_6.html#objectid-valueof caused it but can you not just add the new keyword?

zbarakzai

zbarakzai commented on Nov 11, 2021

@zbarakzai
Author

@IslandRhythms if I don't add the new Keyword Typescript complains about the above error I mentioned.

modified the milestones: 6.0.14, 6.0.15 on Nov 17, 2021
modified the milestones: 6.0.15, 6.0.16 on Dec 6, 2021
vkarpov15

vkarpov15 commented on Dec 9, 2021

@vkarpov15
Collaborator

I took a closer look and this is expected behavior. In MongoDB Node driver 4.x, ObjectId is now a class, and there's no way to allow calling a TS class without new.

The reason why Types.ObjectId() works in JavaScript is that the MongoDB driver's TS compiler doesn't compile to ES6 classes, so the compiled JavaScript looks like this:

/**
 * A class representation of the BSON ObjectId type.
 * @public
 */
var ObjectId = /** @class */ (function () {
    /**
     * Create an ObjectId type
     *
     * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
     */
    function ObjectId(inputId) {
        if (!(this instanceof ObjectId))
            return new ObjectId(inputId);
        // workingId is set based on type of input and whether valid id exists for the input
        var workingId;
        if (typeof inputId === 'object' && inputId && 'id' in inputId) {
            if (typeof inputId.id !== 'string' && !ArrayBuffer.isView(inputId.id)) {
                throw new BSONTypeError('Argument passed in must have an id that is of type string or Buffer');
            }
            if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
                workingId = Buffer.from(inputId.toHexString(), 'hex');
            }
            else {
                workingId = inputId.id;
            }
        }

In theory we might be able to make ObjectId an interface, but for some reason doing the below causes a Property 'ObjectId' does not exist on type 'typeof Types'. error.

export interface ObjectId extends mongodb.ObjectId {
      _id: this;
    }

I'm adding a note to our migration guide.

added
docsThis issue is due to a mistake or omission in the mongoosejs.com documentation
on Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentationtypescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @vkarpov15@root-io@IslandRhythms@zbarakzai

        Issue actions

          Upgrade mongoose from 5 to 6 Value of type 'typeof ObjectId' is not callable. Did you mean to include 'new'? · Issue #10960 · Automattic/mongoose