Search code examples
mongodbtypescriptexpressmongooseexpress-session

Typescript/Mongoose Error: findUser does not exist on type Model


I have the following code. I want to implement authentication using MongoDB, mongoose, express using typescript. I am having a typescript issue. I tried declaring type (maybe incorrectly) for findUser and did not resolve. Any tips?

model.ts

import mongoose, { Schema, Document } from 'mongoose';
import bcrypt from 'bcrypt';

export interface IUser extends Document {
  username: string;
  password: string;
}

const userSchema: Schema = new Schema({
  username: {
    type: String,
    unique: true,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
});

// tslint:disable-next-line: only-arrow-functions
userSchema.statics.findUser = async function (username, password) {
  const user = await User.findOne({ username });
  if (!user) {
    return;
  }

  const isMatch = await bcrypt.compare(password, user.password);
  if (!isMatch) {
    return;
  }
  return user;
};

userSchema.pre<IUser>('save', async function (next) {
  const user = this;
  if (user.isModified('password')) {
    user.password = await bcrypt.hash(user.password, 8);
  }
  next();
});

const User = mongoose.model<IUser & Document>('User', userSchema);
export default User;

auth.ts (route) ERROR:Property 'findUser' does not exist on type 'Model<IUser & Document>'.ts(2339)

import express from 'express';
import User from '../models/user-model';
const router = express.Router();

declare module 'express-session' {
  // tslint:disable-next-line: interface-name
  export interface SessionData {
    user: { [key: string]: any };
  }
}

router.post('/signin', async (req, res) => {
  const { email, password } = req.body;
  const user = await User.findUser(email, password);
  if (user) {
    req.session.user = user._id;
    res.json({
      message: 'You are successfully login',
      auth: true,
    });
  } else {
    res.json({
      message: 'Unable to login',
      auth: false,
    });
  }
});



export = router;

Solution

  • You can set a second generic on the mongoose.model() method which describes the model itself.

    Here we include all of the properties of Model<IUser> and also add your custom function.

    type UserModel = Model<IUser> & {
        findUser: (username: string, password: string) => Promise<IUser | undefined>;
    }
    

    IUser determines the type for the documents in this model, while UserModel determines the type for the model.

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

    Now the type for the method is known. user here gets type IUser | undefined;

    const user = await User.findUser('joe', 'abcd');
    

    Typescript Playground Link