【问题标题】:Argument of type 'Function | FieldValue' is not assignable to parameter of type 'string''Function | 类型的参数FieldValue' 不可分配给“字符串”类型的参数
【发布时间】:2022-01-21 02:06:56
【问题描述】:

我创建了一个用户模型:

class UserModel extends Model {

    static table = 'users'
    static timestamps = true

    static fields = {
        id: { primaryKey: true, autoIncrement: true},
        firstName: DataTypes.STRING,
        lastName: DataTypes.STRING,
        username: DataTypes.STRING,
        email: DataTypes.STRING,
        password: DataTypes.STRING,
        birthday: DataTypes.DATE,
        phoneNumber: DataTypes.INTEGER,
    }
}

当我将现有用户密码与新密码进行比较时:

async signin(user: Pick<User, "username" | "password">){
        const { username, password } = user;

    const existentUser = await UserModel.where('username', username).first()

    if (!existentUser) throw new CustomErrorHandler(Status.NotFound, "User does not exist")

    const isPasswordCorrect = await bcrypt.compare(password, existentUser.password); // Argument of type 'Function | FieldValue' // is not assignable to parameter of type 'string'.
 // Type 'null' is not assignable to type 'string'.
}

我收到了这个 ts 错误:

Argument of type 'Function | FieldValue' is not assignable to parameter of type 'string'.
  Type 'null' is not assignable to type 'string'.

我可以通过使用强制类型来修复它:

const isPasswordCorrect = await bcrypt.compare(password, <string>existentUser.password);

但我正在寻找另一种解决方案。是否有另一种方法可以将 first() 返回的模型转换为 User interface 或其他方式?

【问题讨论】:

    标签: deno denodb


    【解决方案1】:

    为了在模型上使用类型化字段,您需要通过复制字段来设置 Model Records,如下所示:

    class UserModel extends Model {
        static table = 'users'
        static timestamps = true
    
        static fields = {
            id: { primaryKey: true, autoIncrement: true},
            firstName: DataTypes.STRING,
            lastName: DataTypes.STRING,
            username: DataTypes.STRING,
            email: DataTypes.STRING,
            password: DataTypes.STRING,
            birthday: DataTypes.DATE,
            phoneNumber: DataTypes.INTEGER,
        }
    
        id!: string;
        firstName!: string;
        lastName!: string;
        username!: string;
        email!: string;
        password!: string;
        birthday!: Date;
        phoneNumber!: number;
    }
    

    此外,/x/denodb 似乎没有在模型函数上设置通用返回类型。我只需将first() 的返回值转换为UserModel,就可以让您的示例进行类型检查。这是我自己运行的signin() 的版本:

    async function signin(user: Pick<UserModel, "username" | "password">){
        const { username, password } = user;
    
        const existentUser = <UserModel> await UserModel.where('username', username).first();
    
        if (!existentUser) throw new Error("User does not exist")
    
        const isPasswordCorrect = bcrypt.compareSync(password, existentUser.password);
        if (!isPasswordCorrect) throw new Error("Bad password")
    }
    

    需要转换为正确的模型似乎可以在/x/denodb 中改进。我会看看该项目是否接受 PR,但现在上面的内容似乎足够干净 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 2019-09-07
      • 2020-06-02
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 2021-06-16
      相关资源
      最近更新 更多