Answer the question
In order to leave comments, you need to log in
How to find object by field in mongodb?
Good afternoon! How can you have a query by which you need to find an object in the product model:
module.exports.getById = function (req, res) {
Product.findOne({ url: req.params.url }, (err, doc) => {
if (err) {
return errorHandler(res, 404, 'Ошибка! Товар не найден', err)
}
res.status(200).send(doc)
})
}
Answer the question
In order to leave comments, you need to log in
module.exports.getById = async function (req, res) {
try {
const product = await Product.findOne({ url: req.params.url })
if (!product) {
throw new Error()
}
catch {
return errorHandler(res, 404, 'Ошибка! Товар не найден', err)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question