0

I have this route:

// FIXME: This rotes has a unknown bug
router.get('/unverified', async (req, res) => {
  try {
    const unverifiedUsers = await User.find(
      { is_verified: false, is_admin: false },
      { name: 1, code: 1, email: 1, gender: 1 }
    );
    return res.status(200).json(unverifiedUsers);
  } catch (error) {
    return res.status(400).send({ error });
  }
});

And I use it like this:

app.use('/api/users', userRoute);

Everytime I go to http://localhost:5000/api/users/unverified I got this:

{
    "error": {
        "stringValue": "\"unverified\"",
        "kind": "ObjectId",
        "value": "unverified",
        "path": "_id",
        "reason": {}
    }
}

It's strange to me because the other routes work perfectly and that error doesn't give me a reason.

  • 1
    Is that error from your catch object in your question, or a catch all error you have? If the one in the answer, then your error is inside the `try` block and there’s nothing wrong with your route... try simply do `res.json({hi:"there”})` - [here is a repo](https://github.com/balexandre/so65351733) that I did for other question, with what you’re mentioning working... – balexandre Jan 28 '21 at 00:26
  • @balexandre I even tried to console.log() outside the try/catch, and it didn't show up. I also deleted the mongoose logic, and it still... – Sebastian Arrieta Jan 28 '21 at 00:27
  • 1
    We need to know for sure if the error you show is coming from your `catch` block or from some other error mechanism in your server? A few `console.log()` statements in appropriate places will easily answer that question. Put one at the start of your route (before the `try`). Put one right after the `await User.find()`. Put one in the `catch`. Tell us which log statements get hit. FYI, this is elemental debugging/troubleshooting and should generally be something you do before posting here as part of your own troubleshooting. You can also use breakpoints in the debugger to learn this same info. – jfriend00 Jan 28 '21 at 00:57
  • @Phil Before the `try` keyword. Apparently, the callback doesn't execute – Sebastian Arrieta Jan 28 '21 at 01:05
  • Do you have any other routes defined that might match? For example `/api/users/:username` – Phil Jan 28 '21 at 01:08
  • [Ok so I put these `console.log()`](https://i.imgur.com/btJ9kAl.png) In the console, neither of the three appear when I GET to that route with postman. – Sebastian Arrieta Jan 28 '21 at 01:10
  • @Phil Well... I have a `/api/users/:id` route – Sebastian Arrieta Jan 28 '21 at 01:14
  • 1
    That's your problem then. You need to define the specific routes like `/unverified` first. I'm sure there's a duplicate post around here somewhere – Phil Jan 28 '21 at 01:15
  • So the variable routes go below the specific ones? – Sebastian Arrieta Jan 28 '21 at 01:19

1 Answers1

-2

you try to go to the path name called "unverified" but you don't have a route to it

you have to do like that

router.get('/api/users/unverified', async (req, res) => {
try {
const unverifiedUsers = await User.find(
  { is_verified: false, is_admin: false },
  { name: 1, code: 1, email: 1, gender: 1 }
);
 return res.status(200).json(unverifiedUsers);
} catch (error) {
return res.status(400).send({ error });
}
});

also you need to have an route to it with same path name

app.use('/api/users/unverified', userRoute);
  • 1
    I think tou're misunderstanding the OP's code. `userRoute` is a router with the `/unverified` route on it. And, `app.use('/api/users', userRoute);` then makes a proper route for `/api/users/unverified`. – jfriend00 Jan 28 '21 at 00:55