【问题标题】:Search bar get request using Express搜索栏使用 Express 获取请求
【发布时间】:2021-03-12 00:35:00
【问题描述】:

我在我的网站主页上创建了一个搜索栏,用户在搜索栏中输入的任何字符串都将作为获取请求发送,并根据输入我试图在我的 mongo 中查找所有数据db,其中包含名称字段内的输入字符串。

这是我的表格:

<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>

我的 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 });
})

餐厅架构:

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'
    }
})

有人能告诉我为什么它不起作用吗?我在控制台中收到此错误:

(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.

【问题讨论】:

    标签: javascript html node.js mongodb mongoose


    【解决方案1】:

    尝试将您的查询更改为

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

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

    【讨论】:

    • 它似乎将我重定向到正确的页面,但那里没有餐厅。我的 mongo db 中有一家名为“The Rolling Dough”的餐厅。因此,当我在搜索栏中搜索“滚动面团”时,它会将我重定向到 /restaurants/search?resName=rolling+dough 但那里没有餐厅。
    • 搜索确切的名称“The Rolling Dough”会显示正确的结果。你能告诉我如何使它适用于“滚动”或“滚动面团”等搜索吗?
    • @GustyMouse 我知道为时已晚,但您可以使用 toLowerCase() 方法将您的字符串转换为小写字符 ^_^ 我不知道它是否有帮助,但它对我有用
    猜你喜欢
    • 2017-08-19
    • 1970-01-01
    • 2017-10-02
    • 2014-01-27
    • 1970-01-01
    • 2020-12-28
    • 2023-03-12
    • 2013-09-13
    • 1970-01-01
    相关资源
    最近更新 更多