7

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('');
    }
  });
});
8
  • What does console.log(thename) show? Commented Oct 3, 2016 at 2:17
  • with ajax: thename is undefined. without ajax: thename has a value Commented Oct 3, 2016 at 2:21
  • 1
    undefined isn't a string, so that's your problem Commented Oct 3, 2016 at 2:32
  • any idea why it is returning undefined? is my ajax wrong? thanks! Commented Oct 3, 2016 at 2:33
  • 1
    Well, you're posting firstname instead of thename in your ajax, so... Commented Oct 3, 2016 at 2:36

3 Answers 3

3

// thename = ${thename}

LoginUser.find({ $or: [

    {"firstname": {$regex: `${thename}`, $options: 'i'}},
    {"lastname": {$regex: `${thename}`, $options: 'i'}}
]

},

Sign up to request clarification or add additional context in comments.

Comments

0

check your data maybe < thename > is something else than string.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.