Deathspike
Deathspike

Reputation: 8770

mongoose field with the name type

I am trying to validate and save a Passport profile with this structure:

http://passportjs.org/guide/profile/

This is the scheme I came up with:

// Define the schema.
schema = new mongoose.Schema({
    // The name of this user, suitable for display.
    displayName: String,
    // Each e-mail address ...
    emails: [{
        // ... with the actual email address ...
        value: String,
        // ... and the type of email address (home, work, etc.).
        type: String
    }],
    // A unique identifier for the user, as generated by the service provider.
    id: String,
    // The name ...
    name: {
        // ... with the family name of this user, or "last name" in most Western languages ...
        familyName: String,
        // ... with the given name of this user, or "first name" in most Western languages ...
        givenName: String,
        // ... and with the middle name of this user.
        middleName: String
    },
    // The provider which with the user authenticated.
    provider: String
});

The e-mail has a property called 'type', which is reserved for a mongoose type. How do I solve this?

Upvotes: 55

Views: 16315

Answers (2)

lpares12
lpares12

Reputation: 4003

Update for Mongoose 4+

You have to define the typeKey to the schema.

const schema = new Schema({
    //Define a field called type of type String
    type: { $type: String },
    //Define a field called whatever of type Array
    whatever: { $type: Array }
}, { typeKey: '$type' });

So all the schema will use the keyword $type to set the type of the field. And you can safely use type to set the name of the field.

https://mongoosejs.com/docs/guide.html#typeKey

Upvotes: 1

JohnnyHK
JohnnyHK

Reputation: 311835

You need to define the field using an object:

type: {type: String}

Upvotes: 138

Related Questions