Autocomplete

This commit is contained in:
2020-07-21 19:40:27 +02:00
parent 9f4f5347d4
commit 0bc5022fb2
2 changed files with 40 additions and 1 deletions

37
routes/routeSearch.js Normal file
View File

@@ -0,0 +1,37 @@
import express from "express";
import * as services from "../services/services.js";
import Restaurant from "../models/restaurant.js";
import sanitizer from "string-sanitizer";
import restaurant from "../models/restaurant.js";
var router = express.Router();
// AUTOCOMPLETE
router.get("/autocomplete/", (req, res) => {
var query = sanitizer.sanitize.keepUnicode(decodeURI(req.query.string));
const regex = new RegExp(query, "i");
let cities = new Set();
let restaurants = new Set();
Restaurant.find(
{ $or: [{ city: { $regex: regex } }, { name: { $regex: regex } }] },
"name city",
(err, doc) => {
if (err) {
res.send(404);
} else {
doc.forEach((value) => {
cities.add(value.city);
restaurants.add(value.name);
});
res.send({
cities: Array.from(cities),
restaurants: Array.from(restaurants),
});
}
}
).limit(10);
});
export default router;