首页 > 解决方案 > 快速打字稿中的猫鼬$gt运算符?

问题描述

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

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'. 

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

标签: node.jstypescriptmongodbexpressmongoose

解决方案


Date.now()从纪元返回一个整数,UTC 毫秒。用于new Date()获取Date对象(内部仍然是 UTC)。


推荐阅读