无法解决/拒绝承诺

问题描述 投票:0回答:1

我写了一个service方法,该方法允许user在我的再验证API中获得身份验证。 get方法调用此方法:

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');
    }
}

我得到的问题是:

声明的类型既不是'void'也不是'any'的函数必须返回值。

似乎Promise.resolve(customer)不返回任何内容,我也尝试给return加上前缀,但存在相同的问题

typescript api es6-promise restify
1个回答
0
投票
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';
}
© www.soinside.com 2019 - 2024. All rights reserved.