简体   繁体   中英

Search bar get request using Express

I have made a search bar on the home page of my website and whatever string the user enters in the search bar will be sent as a get request, and based on the input I am trying to find all the data in my mongo db which contains the input string inside of the name field.

This is my form:

<main class="main">
    <img src="images/clipart1256.png" width="200" alt="">
    <h1>Welcome!</h1>
    <p>Deliciousness jumping into the mouth</p>
    <form class="search" action="/restaurants/search" method="GET">
        <input class="bar" type="text" placeholder="Search Restaurant..." name="resName">
        <button><i class="fas fa-search"></i></button>
    </form>
</main>

My app.get():

app.get('/restaurants/search', async (req, res) => {
    const { resName } = req.query;
    const restaurants = await Restaurant.find({ $text: { $search: { name: resName } } });
    res.render('restaurants', { restaurants });
})

Restaurant Schema:

const restaurantSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    location: {
        type: String,
        required: true
    },
    image: {
        type: String,
        default: 'https://www.salonlfc.com/wp-content/uploads/2018/01/image-not-found-scaled-1150x647.png'
    }
})

Could someone tell me why it isn't working? I get this error in the console:

(node:8048) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Connection established to database...
(node:8048) UnhandledPromiseRejectionWarning: CastError: Cast to ObjectId failed for value "search" at path "_id" for model "Restaurant"
    at model.Query.exec (C:\Users\...\node_modules\mongoose\lib\query.js:4358:21)
    at model.Query.Query.then (C:\Users\...\node_modules\mongoose\lib\query.js:4450:15)
(node:8048) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8048) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Try change your query to

app.get('/restaurants/search', async (req, res) => {
 const { resName } = req.query;

 const restaurants = await Restaurant.find({$text: {$search: resName}})

 res.render('restaurants', { restaurants });

})

Reference: https://docs.mongodb.com/manual/core/index-case-insensitive/

restaurentSchema.index({ name: "text", description: "text" })

add this line in your restaurent model ( in the last line of course... )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM