【问题标题】:mongoose $gt operator in express typescript?快速打字稿中的猫鼬$gt运算符?
【发布时间】:2021-10-17 06:05:25
【问题描述】:

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

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 项目转换为 typescript,这是我唯一想不通的事情。

【问题讨论】:

    标签: node.js typescript mongodb express mongoose


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-02-05
      • 2020-05-11
      • 2017-04-11
      • 2016-04-01
      • 1970-01-01
      • 2021-01-05
      • 2017-01-20
      • 1970-01-01
      • 2018-06-21
      相关资源
      最近更新 更多