快速打字稿中的猫鼬$ gt运算符?

时间:2020-11-02 00:25:42

标签: node.js typescript express mongoose

我需要找到令牌尚未过期的用户。

try {
    const user = await User.findOne({
      resetToken: passwordToken,
      resetTokenExpiration: { $gt: Date.now() },
      _id: userId,
    });
    if (!user) {
      throw new NotFoundError();
    }

在普通javascript中没有问题。在打字稿中,我遇到此错误:

 Type 'number' is not assignable to type 'Date | undefined'. 

typescript将$gt: Date.now() 评估为类型注释。我正在将node.js项目转换为打字稿,这是我唯一不知道的事情。

1 个答案:

答案 0 :(得分:0)

Date.now()返回一个整数,距离纪元为UTC毫秒。使用new Date()获取一个Date对象(里面仍然是UTC)。