1

I have this code in javascript:

// find the user
  User.findOne({
    name: req.body.name
  }, function(err, user) {

    if (err) throw err;

    if (!user) {
      res.json({ success: false, message: 'Authentication failed. User not found.' });
    } else if (user) {

            //issue token
        }

    })

User.findOne coming for mongoose. But I want to mock that method so I won't talk to database at this point. But want to keep the logic like it is so I need to write my own User.findOne typescript analogue so is behave same.

That is what I come up so far:

export class User { 

    constructor() { }

    public findOne(user: any, next:Function) {
        console.log(1); //getting there
        var fake = {name: "a", password: "p"};

        return fake;
    }
}

and I am importing my code like that

import {User} from '../mock/User'; 

     var login = {name: req.body.name, password: req.body.password} //fake   

     // find the user
      User.prototype.findOne({
        user: login
      }, function(err, user) {
      console.log(2); //not getting there when using my User.findOne from .ts
      ........
}

But code that followed by function(err, user) just not getting executed.

Do you have any ideas how to fix that?

UPDATE: Just tried to use lambdas but still same issue:

User.findOne({user: login}, (err, user) => {
   console.log("test"); //- this code inside function just not executed
})

2 Answers 2

2

If you want the method to be called directly from the User class and not from an instance you need to declare it as static.

public static findOne(user: any, next:Function) {
        console.log(1); //getting there
        var fake = {name: "a", password: "p"};
        next(undefined,fake); //this will call the function you passed as lambda or regular function
}

This way you can call the method like this.

User.findOne({},(error,result)=>{
   //using a lambda here to make sure you have no issues with 'this' but you can also pass a regular function object.
  console.log(result);
  console.log(error);
}
5
  • is there any reasons why next(); is before return fake; ?
    – Sergino
    Commented Feb 7, 2016 at 10:41
  • once you call return in a method, the method will stop executing so you cant do anything after that. You might need to pass your fake object as a parameter to the next method because using return will not be the same as the mongoose method. Commented Feb 7, 2016 at 10:42
  • oh so then my method User.findOne won't make much seance if I cant get return from it
    – Sergino
    Commented Feb 7, 2016 at 10:52
  • check my updated answer. your method will pass your fake object to the callback and the callback can handle it any way you want. To reflect the way mongoose works, an undefined value is passed first for the error case and your data is the second parameter. Commented Feb 7, 2016 at 10:54
  • just did that. that thks.
    – Sergino
    Commented Feb 7, 2016 at 10:55
1

Try defining at user.ts a class named User with an static method named findOne(err, user):

user.ts

export class User {

  constructor(){}

  static findOne(query, user: Function) {
    // whatever
  }
}

use it as:

import * as User from './path/user'

User.findOne({...}, (err, user) => {
   // whatever
})

Apart, a tip which I find so useful: use typescript to define the signature of your callback instead of just Function, defining the arguments of your cb; for example:

user.ts

export class User {

  // callback function has 2 arguments: err and user (you can specify their types too
  static findOne(query: any, cb: (err: any, user: any) => void) {
    // whatever
  }
}
3
  • having issues still. the lembdas is not working for me
    – Sergino
    Commented Feb 7, 2016 at 10:30
  • also when using import * as User from '../mock/User'; getting an error property findOne does not exists on type typeof User
    – Sergino
    Commented Feb 7, 2016 at 10:35
  • Sounds as a configuration issue for me, not a problem with the language itself. What tool are you using for compiling ts into js? Commented Feb 7, 2016 at 11:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.