mongoose $gt operator in express typescript?

Viewed 207

I need to find users whose tokens have not expired yet.

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

in plain javascript it had no issue. in typescript i am getting this error:

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

typescript evaluates $gt: Date.now() as type annotation. I am converting my node.js project to typescript and this is the only thing that I could not figure out.

1 Answers

Date.now() returns an integer, UTC milliseconds from the epoch. Use new Date() to get a Date object (it's still UTC on the inside).

Related