我在 TypeScript 中使用 Sequelize 在 Express 中实现 API。使用关联 mixin 返回的值时,我遇到了与类型相关的错误。
这是我定义的数据模型的一部分:
export class User extends Model {
id!: number;
username!: string;
email!: string;
password!: string;
active!: boolean;
createdAt!: Date;
updatedAt!: Date;
}
export class UserSession extends Model {
token!: string;
userId!: number;
expiresAt!: Date;
renewalHours!: number;
getUser!: BelongsToGetAssociationMixin<User>;
}
以下是我在 session 期间(在 Express 中间件内)尝试验证请求的方式:
UserSession.findOne({ where: { token: req.body.token }, include: [User] })
.then(session => {
if (session) {
const now = moment();
if (session.expiresAt > now.toDate()) {
session.set('expiresAt',
now.add(session.renewalHours, 'hours').toDate());
session.save();
req.user = session.getUser();
next();
} else {
session.destroy();
res.status(400).json({ msg: 'Session expired' });
}
} else {
res.status(403).json({ msg: 'Not logged in' });
}
});
我已经通过
UserSession
将 User
与 UserSession.belongsTo(User)
相关联(细节省略)。特定的行
req.user = session.getUser();
从 TypeScript 产生以下错误:
Type 'Bluebird<User>' is missing the following properties from type 'User': id, username, email, password, and 26 more.
req
是一个 Express Request
。我已经向它添加了一个 user
字段(出于 TypeScript 的目的),如下所示:declare global {
namespace Express {
export interface Request {
user?: User;
session?: UserSession;
}
}
}
我知道 Sequelize 现在建立在 Bluebird Promise 框架之上,所以我怀疑这与我的类型定义有关。似乎 TypeScript 不认为
UserSession.getUser
会返回具有 User
属性的东西。为什么不?
最佳答案
正如预测的那样,这是对 Sequelize 的 mixin 函数工作方式的一个非常简单的误解。与 Sequelize 中的所有内容一样,它们当然是使用 Bluebird 的异步调用!因此,UserSession.getUser
不返回 User
而是返回用户的 Promise。线路固定如下:
req.user = await session.getUser();
当然,将此函数标记为
async
。这也可以使用 session.getUser().then( ... )
来完成。
关于node.js - 使用 Sequelize 关联时缺少属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62122612/