Answer the question
In order to leave comments, you need to log in
How to send JSON data to client?
HTML:
<form className="header__search-form" onSubmit={() => search()} action="/search" method="POST">
<span>Поиск</span>
<input type="text" className="header-search-input" name="q" />
</form>
async function search() {
const {data} = await axios.post('/search');
setProducts(data);
}
router.post('/', (req, res) => {
product.find(async(err, data) => {
if (err) res.status(400).send(err);
else {
if (req.body.q) {
const newData = await data.filter(item => item.name.toLowerCase().search(req.body.q.trim().toLowerCase()) !== -1);
res.status(200).send(newData);
}
}
})
})
Answer the question
In order to leave comments, you need to log in
router.post('/', (req, res) => {
{query} = req.body
product.find(async(err, data) => {
if (err) res.status(400).send(err);
else {
if (query) {
const newData = await data.filter(item => item.name.toLowerCase().search(query.trim().toLowerCase()) !== -1);
res.json(newData);
}
}
})
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question