The problem is, I'm getting an error in my Console:
{ [MongoError: $regex has to be a string]
name: 'MongoError',
message: '$regex has to be a string',
waitedMS: 0,
ok: 0,
errmsg: '$regex has to be a string',
code: 2 }
Basically I'm using Ajax to get data from my MongoDB, where I'm searching for a user. Without Ajax, my search function is working correctly, but I want to search for a user without the need of refreshing the web-page and just fill up my HTML. Here is all my code:
Server code:
app.post("/searchresult", function(req, res){
var thename = req.body.thename;
LoginUser.find({
$or: [
{"firstname": {$regex: thename, $options: 'i'}},
{"lastname": {$regex: thename, $options: 'i'}}
]
}, function(err, searchedUser){
if(err){
console.log(err);
res.redirect("back");
} else {
res.render("searchprofile", {foundUser: searchedUser});
}
});
});
HTML code:
<form class="form-inline" id="searchform" action="/searchresult" method="POST">
<input type="text" class="form-control" placeholder="Search people" name="thename" id="searchinput">
<button type="submit" class="btn btn-default">Submit</button>
</form>
JQuery code:
$("#searchform").on('submit', function(e){
e.preventDefault();
var searchInp = $("#searchinput");
$.ajax({
url: '/searchresult',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ firstname: searchInp.val() }),
success: function(response){
console.log(response);
searchInp.val('');
}
});
});
The problem is, I'm getting an error in my Console:
{ [MongoError: $regex has to be a string]
name: 'MongoError',
message: '$regex has to be a string',
waitedMS: 0,
ok: 0,
errmsg: '$regex has to be a string',
code: 2 }
Basically I'm using Ajax to get data from my MongoDB, where I'm searching for a user. Without Ajax, my search function is working correctly, but I want to search for a user without the need of refreshing the web-page and just fill up my HTML. Here is all my code:
Server code:
app.post("/searchresult", function(req, res){
var thename = req.body.thename;
LoginUser.find({
$or: [
{"firstname": {$regex: thename, $options: 'i'}},
{"lastname": {$regex: thename, $options: 'i'}}
]
}, function(err, searchedUser){
if(err){
console.log(err);
res.redirect("back");
} else {
res.render("searchprofile", {foundUser: searchedUser});
}
});
});
HTML code:
<form class="form-inline" id="searchform" action="/searchresult" method="POST">
<input type="text" class="form-control" placeholder="Search people" name="thename" id="searchinput">
<button type="submit" class="btn btn-default">Submit</button>
</form>
JQuery code:
$("#searchform").on('submit', function(e){
e.preventDefault();
var searchInp = $("#searchinput");
$.ajax({
url: '/searchresult',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ firstname: searchInp.val() }),
success: function(response){
console.log(response);
searchInp.val('');
}
});
});
Share
Improve this question
edited Feb 25, 2021 at 7:45
osundblad
2,6811 gold badge30 silver badges36 bronze badges
asked Oct 3, 2016 at 2:03
JohnJohn
4801 gold badge9 silver badges24 bronze badges
8
-
What does
console.log(thename)
show? – JohnnyHK Commented Oct 3, 2016 at 2:17 - with ajax: thename is undefined. without ajax: thename has a value – John Commented Oct 3, 2016 at 2:21
-
1
undefined
isn't a string, so that's your problem – JohnnyHK Commented Oct 3, 2016 at 2:32 - any idea why it is returning undefined? is my ajax wrong? thanks! – John Commented Oct 3, 2016 at 2:33
-
1
Well, you're posting
firstname
instead ofthename
in your ajax, so... – JohnnyHK Commented Oct 3, 2016 at 2:36
3 Answers
Reset to default 2// thename = ${thename}
LoginUser.find({ $or: [
{"firstname": {$regex: `${thename}`, $options: 'i'}},
{"lastname": {$regex: `${thename}`, $options: 'i'}}
]
},
check your data maybe < thename > is something else than string.
This might also be an issue with the actual type inside mongoDB. I had this error when I was trying to query on a boolean, so my schema was
const schema = {
email:string,
active: boolean,
role:string
}
but I was trying to query on all of these, with $or.
if (typeof query === 'string') {
queryObject['$or'] = [
{ email: { $regex: `${query}`, $options: 'i' } },
{ role: { $regex: `${query}`, $options: 'i' } },
{ active: { $regex: `${query}`, $options: 'i' } },
]
}
So the issue was with the active it's a boolean, however I was trying to query it with a regex and this cause the issue.
Summary:
- Check your schema types if you're using mongoose, or your data type inside mongodb.
- Query also should be a string for sure.