Node.js GET请求始终默认为/(?:)/i-如何使其成为';未定义的';?-关于这个话题的第二个问题
我在这里得到了一些很好的帮助,可以修复总是默认为/(?:)/I的GET请求。我现在可以将GET设置为未定义 但是,这仅在我的数据库中搜索一个字段“BusinessName”时有效。 我的问题是,如何搜索多个字段,比如“BusinessName”和“Address”。 以下是仅搜索一个字段时使用的代码:Node.js GET请求始终默认为/(?:)/i-如何使其成为';未定义的';?-关于这个话题的第二个问题,node.js,mongodb,express,get,xregexp,Node.js,Mongodb,Express,Get,Xregexp,我在这里得到了一些很好的帮助,可以修复总是默认为/(?:)/I的GET请求。我现在可以将GET设置为未定义 但是,这仅在我的数据库中搜索一个字段“BusinessName”时有效。 我的问题是,如何搜索多个字段,比如“BusinessName”和“Address”。 以下是仅搜索一个字段时使用的代码: app.get("/widget", function(req, res) { // This returns just one restaurant and doe
app.get("/widget", function(req, res) {
// This returns just one restaurant and doesnt do partial string matching
/* let test = req.query.search */
// This returns up to 200 results that partially match the search term
/* let test = new RegExp (req.query.search, 'i'); */
// This sets the query to undefined, to start with
const test = (req.query.search)
? new RegExp(req.query.search, 'i')
: undefined
Restaurant.find({
BusinessName: test
}, null, {
limit: 200
}, function(err, foundRestaurant) {
console.log(test);
if (foundRestaurant) {
/* res.send(foundRestaurant) */
res.render("widget", {
foundRestaurant
})
} else {
res.render("widget", "No restaurants matching that title was found.");
}
});
});
下面是我使用“AddressLine3”和“AddressLine4”字段使其工作的失败尝试:
app.get("/town_widget", function(req, res) {
const test3 = (req.query.search)
? new RegExp(req.query.search, 'i')
: undefined
const test4 = (req.query.search)
? new RegExp(req.query.search, 'i')
: undefined
Restaurant.find({
$or:[{
AddressLine3: test3
},
{
AddressLine4: test4
}]},
null, {
limit: 2
},
function(err, foundTown) {
if (foundTown) {
console.log(foundTown);
res.render("town_widget", {
foundTown
})
} else {
res.render("town_widget", "No town or city matching that input was found/no restaurants found in the town specified.");
}
});
});
琼斯谢谢你的帮助。事实上,我发现我做错了什么,AddressLine4在我的DB中不存在,因此将它包含在我的mongo请求中导致了错误-我曾经有过AddressLine4,但现在我没有了。使用OR操作员,现在一切都很好,干杯您喜欢MongoDb吗?如果是这样,请将该标记添加到您的问题中,并查找
$和操作员。嗨,O.琼斯,我的数据中的城镇有时位于AddressLine3,有时位于AddressLine4。所以我使用了Or运算符。我应该使用And操作符吗?如果我使用Or运算符,它会忘记查询一开始是未定义的,并且在输入搜索查询之前,总是在加载页面时进行搜索