【发布时间】: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