D
D
Devero972021-03-14 22:36:33
Node.js
Devero97, 2021-03-14 22:36:33

How to handle 2 get requests at once?

I have pages: category and product (card). I send a get request with two dynamic parameters. For example, localhost:3000/category-example/jeans. After that I want to check if there is a second request (jeans) in the first request (category-example). Each card contains an array with the IDs of the categories in which it is present. When switching to a product, I have to check whether this product exists in the category the user went through. To show an error to the user when going to localhost:3000/category-error/jeans. How it is possible to implement it? I came up with just to sort through the array with IDs and if my category ID is not there, then give an error. But is it right?
Here is the processing of the request:

exports.getCard = asyncHandler(async (req, res, next) => {

  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)
    );
  }

  const tag = card.tags.filter(item => req.params.tag == item.tag);
  if (tag.length == 0) {
    return next(
      new ErrorResponse(`Not found with slug of ${req.params.card}`, 404)
    );
  }

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

First, I look to see if the product itself is there and, depending on the answer, I display either an error or redirect to the page.
Then I take the categories from the card (product) and match them against my dynamic category parameter. And if it is, then everything is good, and if not, I display an error.
Am I doing the right thing in this case?

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