D
D
Devero972021-03-01 12:05:50
Node.js
Devero97, 2021-03-01 12:05:50

Why doesn't an error occur on a non-existent request?

There are 2 pages. One with a tag containing a list of cards, the other page - the card itself (detailed information about the card).
When switching to a tag, I make the following route: When requesting, for example, localhost:3000/na-karty, I display a list of cards for this tag (and various information about this tag). Then I go to the card itself with this route: When I request, for example, localhost:3000/na-karty/kartochka-one, I get a card with detailed information. But now the problem is that when I change exactly the tag in the request, for example, localhost:3000/na-kartochky/kartochka-one , I don't get an error. I have a main page where I display all the cards that are in the database using this route:
router.route("/:tag").get(getTag);

router.route("/:card").get(getCard);



router.route("/").get(getCards);
And I display them by a certain tag. Let's say for a tag that is available on request localhost:3000/rating . When moving from the main page to a card with detailed information, I make the following request localhost:3000/rating/kartochka-one . Everything is cool, but when I change exactly the tag request, namely, localhost:3000/omg-kak-tak/kartochka-one, I don't get an error. All also the page is available with detailed card info.

To fix this, when requesting a card, I check not only if there is a card, but also if there is a tag. Otherwise I throw an error. The processing is like this:

exports.getCard = asyncHandler(async (req, res, next) => {
  const tag = await Tag.findOne({ tag: req.params.tag });
  if (!tag) {
    return next(
      new ErrorResponse(`Not found with slug of ${req.params.card}`, 404)
    );
  }

  const card = await Card.findOne({ card: req.params.card })
    .populate("tags")
    .populate("reviews");

  if (!card) {
    return next(
      new ErrorResponse(`Not found with slug of ${req.params.card}`, 404)
    );
  }

  res.status(200).json(card);
});

But the problem is that I do not get the tag parameter, but only my card, which I switched to. And how to solve such a problem? It seems that you can solve it only by passing the tag route parameter there, but you can’t just pass it to the get request. What can be done? Help me please!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question