Saturday 6 May 2017

MongoDB won't return token string search results using mongoose or mongo shell

I'm trying to write a reset password function, so I want to find the user by resetPasswordToken. The result returns that the token expired (no result returned), so I verified the token in the db by going into mongo and executing the same query.

The backend code:

User
    .findOne({resetPasswordToken: token}, function(err, user) {
      if (err) {
        console.log("Server error searching for user.", err);
        callback("Server error searching for user.");
      } else {
        console.log(user);
        if(user){
          console.log("User exists!  Create token for user ", user);
          saveNewPass(user, password, callback);
        } else { 
          console.log("The password reset token has expired.  Request a new reset password link.");
          callback(null, {message: null, error: 'The password reset token has expired.'});
        }
      }
  });

It returns the password reset token has expired, which means there is no result coming back.

In mongo shell, I first searched for the record using:

db.users.find({username: "myusername"})

Which returns:

{ 
   "_id" : ObjectId("5908913f6e738b1eef729119"), 
   "username" : "myusername", 
   "password" : "$2a$10$jL5BuxYSA/jiiGiwCxKAGO7ISNaOiFTqX1.RUqJrcVA7x4kgfVD8u", 
   "resetPasswordExpires" : ISODate("2017-05-06T23:33:34.036Z"), 
   "resetPasswordToken" : "65bce3ff4183a9e78eb96f71c2f5ea6449369259" 
}

Then, I copied and pasted the string value of the resetPasswordToken attribute, and ran this query:

db.users.find({resetPasswordToken: "65bce3ff4183a9e78eb96f71c2f5ea6449369259"})

I get NOTHING. What is the deal? I originally thought it was that I was doing date comparison in my search, so I removed it. This is strange.



via user3561890

No comments:

Post a Comment