Search code examples
typescriptapies6-promiserestify

Cannot resolve / reject a Promise


I wrote a service method which allow an user to get a authenticate in my restify API. The get method invoke this:

public async auth(email: string, password: string): Promise<Customer> {
    let connection = await DatabaseProvider.getConnection();
    const customer = await connection.getRepository(Customer).findOne({ email });

    try {
        let isMatch = await bcrypt.compare(password, customer!.password);

        if (!isMatch) throw 'Password did not match';
        Promise.resolve(customer);
    } catch (err) {
        Promise.reject('Authentication failed');
    }
}

the problem's that I get:

A function whose declared type is neither 'void' nor 'any' must return a value.

seems that Promise.resolve(customer) does not return anything, I also tried to prefix with return but same problem


Solution

  • public async auth(email: string, password: string): Promise<Customer> {
        let connection = await DatabaseProvider.getConnection();
    
        const customer = await connection.getRepository(Customer).findOne({ email });
    
        let isMatch = await bcrypt.compare(password, customer!.password);
    
        if (!isMatch) throw 'Password did not match';
    
        if(customer)
          return customer;
    
        throw 'Customer is undefined';
    }
    

    This should work i guess...