如何在Express JS中使用特定字段而不是ID进行查询

医师

以下是我按城市查询搜索的代码。我的帖子中找不到我>我需要能够搜索特定城市并获取与城市匹配的字段的所有记录。例如,如果城市为“市区”,则我希望包含市区“市区”的所有记录

            router.get("/search", (req, res, next) =>{
            const city = req.query.city;
            Facility.findAll(city)
                .select('name type mobile price streetName city state _id')
                .exec()
                .then(docs => {
                console.log("From database", docs);
                if (docs) {
                    res.status(200).json({
                        facility: docs
                    });
                } else {
                    res
                    .status(404)
                    .json({ message: "No valid entry found for provided City" });
                }
                })
                .catch(err => {
                console.log(err);
                res.status(500).json({ error: err });
                });

            });
医师

下面是工作代码:

router.get('/search', (req, res) => {
const city = req.query.city
    Facility.find({city})
        .select('name type mobile price streetName city state')
            .exec((err, docs) => {
                if (err) {
                    return res.status(500)
                    .json({ message: 'error querying cities', error: err });
                }
                if (!docs) {
                    return res.status(404)
                    .json({ message: 'No valid entry found for provided City' });
                }
                return res.status(200)
                    .json({
                    count: docs.length,
                    facility: docs
                });
            })

});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Nodejs express 中使用 flash

如何在Express中使用jsTree

如何在 Express 中使用静态类

如何在 Express 中使用请求进行 POST

如何在MongoDB中使用自定义字段对分页进行索引和排序,例如:名称而不是ID

如何在express js中通过id更新特定记录

如何在express js中使用静态变量?

如何在Express.js中使用Less?

如何在Express JS中使用MVC ViewBag概念?

如何在Express中使用Discord.JS素材

如何在Node.js和Express中使用PATCH

express JS 应用程序 - 我们如何在 express 中使用普通的 HTML 而不是模板引擎

如何在 Node.js 中使用 express 顺序发送 mysql 查询

如何在DetailView中使用PK(主键)字段以外的其他字段进行查询

如何在express.js中编写findOneAndUpdate查询?

如何在express.js中解决承诺的mysql查询?

如何在 Python 中使用文本字段对 MySQL 进行查询?

如何在AngularJS&Express中使用其他导出?

如何在html中使用express中的变量?

如何在Express中使用npm安装的引导程序?

如何在ejs / express中的Globals变量中使用函数?

如何在Socket.io和Express中使用群集

如何在Express中使用connect-orientdb模块

如何在Express中使用mongoClient.connect?

如何在Express和Mongo DB中使用异步等待

如何在 Express 和 MongoDB 中使用异步和等待

如何在Express中使用原始中间件

如何在Express应用中使用DefinitetlyTyped定义

如何在Java mongodb驱动程序中使用“ _id”字段查询文档?