Answer the question
In order to leave comments, you need to log in
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);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question