Definitelytyped: Mongoose Type Definitions (@Types v4.5.39) ObjectID creation missing property

Created on 31 Oct 2016  Â·  9Comments  Â·  Source: DefinitelyTyped/DefinitelyTyped

I'm not sure if I'm doing something wrong or what (the definitions haven't been updated in awhile, and I swear this was working a week ago or so).

In my Schema, I have a property defined as family: {type: mongoose.Schema.Types.ObjectId, required: true}. In my express controller, I create a new object based on this schema: var user = new User(request.body} where User is an import of the mongoose user model. I then check for the family property, and if it exists (populated on the frontend), then just save the document, otherwise, generate an ID. This is where I'm getting the type error.

I set the value like: user.family = new mongoose.Types.ObjectId; and I get this as a Typescript error: TS2322: Type 'ObjectID' is not assignable to type 'ObjectId'. Property 'auto' is missing in type 'ObjectID'.

I've tried several configurations to get around it, including using the mongoose.Schema.Types.ObjectId, but I can't seem to get past it. Am I an idiot, or is this an issue with the definition?

Controller Method:

export var create = (request: express.Request, response: express.Response, next: express.NextFunction) => {
    var user = new User(request.body);

    if (!user.family) {
        user.family = new mongoose.Types.ObjectId;
    }

    user.save((error) => {
        if (error) {
            return next(error);
        } else {
            response.json(user);
        }
    })
};

Schema:

var schema: mongoose.Schema = new mongoose.Schema({
    firstName: { trim: true, type: String },
    lastName: { trim: true, type: String },
    family: { required: true, type: mongoose.Schema.Types.ObjectId }
});

Most helpful comment

Gotcha, thanks. To help others I'm posting this small snippet.

import {Document, model, Model, Schema, Types} from 'mongoose';

const fooSchema = new Schema({
  Bars: [{
    BarId: Schema.Types.ObjectId
  }]
});

export interface IFoo extends Document {
  Bars: [{
    BarId: Types.ObjectId
  }]
}

const Foos: Model<IFoo> = model<IFoo>('Foo', fooSchema);

Sample Call

Foos.findById("abc")
  .exec()
  .then((myFoo) => {
    myFoo.Bars.push({
      BarId: Types.ObjectId("123ABC")
    });
    return myFoo.save();
  }

All 9 comments

please send a pull request. I'll review it.

So I'm not sure if this is an issue or not. I spent the day playing around with various iterations of it, and managed to get it working by changing the type in my model interface. I was using mongoose.Schema.Types.ObjectId, once I changed that to mongoose.Types.ObjectId everything works...

@vvakame It's not definition problem. It can be closed.

@digital216 mongoose.Types.ObjectId is to assert for fields of schema, not build an ObjectId instance. But It's sorry to say it, please read mongoose document clearly.

@TonyPythoneer can you please clarify the difference between Types.ObjectId & Schema.Types.ObjectId. I've read the the docs but they are not really doing a great job explaining the difference.

Looking at the following snippet, I'm even more confused (since both constructs are used):
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/mongoose/mongoose-tests.ts#L526

@muratsu In general, if the official document doesn't mention, it means it doesn't offer for users. For example, Types, it only mentions in API, not in the guide.

If you want to know, I can try to clarify this question.

Gotcha, thanks. To help others I'm posting this small snippet.

import {Document, model, Model, Schema, Types} from 'mongoose';

const fooSchema = new Schema({
  Bars: [{
    BarId: Schema.Types.ObjectId
  }]
});

export interface IFoo extends Document {
  Bars: [{
    BarId: Types.ObjectId
  }]
}

const Foos: Model<IFoo> = model<IFoo>('Foo', fooSchema);

Sample Call

Foos.findById("abc")
  .exec()
  .then((myFoo) => {
    myFoo.Bars.push({
      BarId: Types.ObjectId("123ABC")
    });
    return myFoo.save();
  }

@muratsu looks right
@digital216 I believe this is how it works. When you create a new Schema:

var schema: mongoose.Schema = new mongoose.Schema({
    ...
    family: { required: true, type: mongoose.Schema.Types.ObjectId }
});

Mongoose uses mongoose.Schema.Types.ObjectId internally for creating schemas.

When you do:

var user = new User(request.body);

The user that you created will have an interface that probably looks like this:

interface IUser extends Document {
  ...
  family: mongoose.Schema.Types.ObjectId;
}

Now mongoose.Schema.Types.ObjectId and mongoose.Types.ObjectId are different types so when you assign to family:

user.family = new mongoose.Types.ObjectId;

It should throw an error. Since mongoose.Types.ObjectId is an alias to mongodb.ObjectID, you'll get that confusing ObjectID is not assignable to ObjectId error.

@digital216 @muratsu I'll put this in the readme somewhere. To summarize,

For creating schemas:

const fooSchema = new Schema({
  Bars: [{
    BarId: Schema.Types.ObjectId
  }]
});

For type checking:

export interface IFoo extends Document {
  Bars: [{
    BarId: Types.ObjectId
  }]
}

Thanks!! That'll help any newbies looking into this!

On Mon, Nov 21, 2016, 8:13 PM Simon Xiong [email protected] wrote:

@digital216 https://github.com/digital216 @muratsu
https://github.com/muratsu I'll put this in the readme somewhere. To
summarize,

For creating schemas:

const fooSchema = new Schema({
Bars: [{
BarId: Schema.Types.ObjectId
}]
});

For type checking:

export interface IFoo extends Document {
Bars: [{
BarId: Types.ObjectId
}]
}

—

You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/12385#issuecomment-262118503,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AK5k5q6dKAkYJfsriAC1rLBwOFp-NbOYks5rAkGtgaJpZM4KlHpG
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lilling picture lilling  Â·  3Comments

Loghorn picture Loghorn  Â·  3Comments

Zzzen picture Zzzen  Â·  3Comments

stevermeister picture stevermeister  Â·  3Comments

victor-guoyu picture victor-guoyu  Â·  3Comments