0

I am trying to query against the following mLab Database document in my React component:

{
    "_id": {
        "$oid": "5e7a4fece9cee40348a63dff"
    },
    "userId": "5e7a4fc6e9cee40348a63dfe",
    "timestamp": {
        "$date": "2020-03-24T18:10:43.955Z"
    },
    "isDeleted": false,
    "__v": 0
}

Below is what I'd like to do but I can't seem to get access the $oid value with my query. I have tried wrapping the token value in mongoose.Types.ObjectId(token) as well as the answer here. But if I use $oid I get an error saying it cannot be used.

const token = req.body.token;

UserSession.find(
      {
        _id: token,  // Get an error here
        isDeleted: false
      },
      (err, sessions) => {
        if (err) {
          return res.send({
            success: false,
            message: "Server Error..." + err
          });
          if (sessions.length != -1) {
            return res.send({
              success: true,
              message: "Good"
            });
          }
        }
      }
    );

I am totally out of ideas and couldn't find anything that works here on SO or in mLab/MongoDB docs. Any help much appreciated.

mikeym
  • 5,705
  • 8
  • 42
  • 62
  • try logging the value of `token`, you may find that it was stringified or otherwide modified by the request. – Joe Mar 25 '20 at 19:23
  • @joe - thanks for the input.I had already logged out the value of token and it seemed fine.I have,however,realized that the failure was being caused by the first if statement not being closed correctly. It was a simpler fix than I first thought.It works now. – mikeym Apr 13 '20 at 18:44

0 Answers0