scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

[TypeError: Class constructor Document cannot be invoked without 'new'] when using ts-node #104

Closed guysenpai closed 9 months ago

guysenpai commented 7 years ago

I got this error TypeError: Class constructor Document cannot be invoked without 'new' when running my code in typescript with ts-node This is my camo model user.model.ts

'use strict';

import { Document as CamoDocument, DocumentSchema, SchemaTypeExtended } from 'camo';
import { genSalt, hash, compare } from 'bcryptjs';

export interface UserSchema extends DocumentSchema {
  email: string;
  password: string;
  fullname: string;
  displayName: string;
  picture: string;
  facebook: string;
  google: string;
  twitter: string;
  providers: string[];
  resetToken: string;
  resetTokenExpiration: number;

  save(): Promise<UserSchema>;

  hashPassword(password: string, callback: any): any;
  comparePassword(password: string, callback: any): any;
}

export class User extends CamoDocument<UserSchema> {
  email: SchemaTypeExtended | string = { type: String, unique: true };
  password: SchemaTypeExtended | string = String;
  fullname: SchemaTypeExtended | string = String;
  displayName: SchemaTypeExtended | string = String;
  picture: SchemaTypeExtended | string = String;
  facebook: SchemaTypeExtended | string = String;
  google: SchemaTypeExtended | string = String;
  twitter: SchemaTypeExtended | string = String;
  providers: SchemaTypeExtended = [String];
  resetToken: SchemaTypeExtended = String;
  resetTokenExpiration: SchemaTypeExtended = Number;

  constructor() {
    super();
  }

  static collectionName() {
    return 'users';
  }

  hashPassword(password: string, callback: any) {
    genSalt(10, (error: Error, salt: string) => {
      if (error) {
        return callback(error);
      }

      hash(password, salt, (err: Error, hash: string) => {
        if (err) {
          return callback(err);
        }
        callback(err, hash);
      });
    });
  }

  comparePassword(password: string, callback: any) {
    compare(password, <string>this.password, (error: Error, isMatch: boolean) => {
      if (error) {
        return callback(error);
      }
      callback(error, isMatch);
    });
  }
}

the code which use the model

router.post('/signup', (req: Request, res: Response) => {
  User.findOne<UserSchema>({ email: req.body.email }).then(user => {
    if (user) {
      return res.status(409).send({
        success: false,
        message: 'Sorry, this email is already taken'
      });
    }

    const newUser = User.create({
      fullname: req.body.fullname,
      displayName: req.body.displayName,
      email: req.body.email,
      password: req.body.password,
      providers: ['local']
    });

    newUser.save().then(u => {
      res.status(201).send({
        success: true,
        token: AuthService.createToken(newUser)
      });
    });
  });
});

My testing code which show error

it('should sign up a new user', () => {
    chai.request(app).post('/auth/signup')
      .send({
        email: 'guysenpai@test.io',
        password: 'guysenpai',
        fullname: 'Guy Senp@i',
        displayName: 'Khal El'
      })
      .then(res => {
        res.should.be.json;
        res.should.have.status(201);
        res.body.success.should.be.true;
        res.body.token.should.not.be.empty;
      });
  });

The test is a success but it show in console the TypeError above