Daniel Sava
Daniel Sava

Reputation: 250

Mongoose: Property 'methods' does not exist on type 'Schema'. Did you mean 'method'?

Suddenly my code started throwing errors regarding methods on mongoose schema.

I have the following Document and Schema

import mongoose from 'mongoose';
import bcrypt from 'bcrypt';

interface UserDoc extends mongoose.Document {
    firstName: string,
    lastName: string,
    email: string,
    password: string,
    domainId: mongoose.Types.ObjectId,
    scope: string,
    tc: boolean,
    newsletter: boolean
}

const userSchema = new mongoose.Schema({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    domainId: {
        type: mongoose.Types.ObjectId,
        ref: 'Domain',
        required: true
    },
    scope: {
        type: UserScopes,
        required: true
    },
    verified: {
        type: Boolean,
        default: false
    },
    tc: {
        type: Boolean,
        default: false
    },
    newsletter: {
        type: Boolean,
        default: false
    }
}, {
    timestamps: true,
    strict: true
});

userSchema.methods.toJSON = function () {
    var object = this.toObject();
    delete object.password;
    delete object.__v;

    object.id = object._id;
    delete object._id;

    return object;
}

userSchema.pre('save', async function(next) {
    const salt = await bcrypt.genSalt();
    const hashedPassword = await bcrypt.hash(this.get('password'), salt);
    
    this.set('password', hashedPassword);

    next();
});

userSchema.methods.comparePasswords = async function (triedPassword: string, savedPassword: string) {
    const result = await bcrypt.compare(triedPassword, savedPassword);
    return result;
}

userSchema.index({
    email: 1,
    domainId: 1
}, { unique: true });

const User = mongoose.model<UserDoc>('User', userSchema);

export { User };

Everything was working just fine an hour ago, but after a restart of the code, I get the following errors:

src/models/user.ts(55,4): error TS2554: Expected 0-1 arguments, but got 2.
  => This relates to timestamps and strict settings.

src/models/user.ts(60,12): error TS2551: Property 'methods' does not exist on type 'Schema'. Did you mean 'method'?
  => This relates to toJSON method.

src/models/user.ts(77,5): error TS2554: Expected 1 arguments, but got 0.
  => This relates to the call made to next inside pre('save').

src/models/user.ts(80,12): error TS2551: Property 'methods' does not exist on type 'Schema'. Did you mean 'method'?
  => This relates to comparePasswords method.

I have no idea what is causing the issue.

MENTION: I am running the application using Docker, and I've also tried compiling with an older version of mongoose, and with the latest version as well.

Upvotes: 1

Views: 581

Answers (1)

Sumit De
Sumit De

Reputation: 176

Today mongoose has released a new version and removed many of functionalities. I suggest to use older version of mongoose. Please share package.json so that I can specify the version.

Upvotes: 1

Related Questions