简体   繁体   中英

Searching Users in Mongodb Database

I am using mongdb and have a database of users. I would like to allow users to search the database for other users based on their username. So far, I have tried and had no luck achieving this.

Here is my front-end HTML:

<h1>Search Users</h1>

<form class="form-inline" method="POST" action="/users/search">
  <div class="form-group">
    <input type="text" name="user_query" class="form-control" placeholder="Find Users By Username...">
  </div>
  <button type="submit" class="btn btn-primary">Search</button>
</form>

And my server side code in app.js:

// Seach Page
app.get('/', function(req, res, next){
  res.render('searchusers');
});

// Search Processing
app.post('/users/search', function(req, res, next){
  var query = req.body.user_query;

  var result = db.users.findOne({"username":"user_query"})

  console.log('Searched for user: '+result);
});

My collection in the database that stores users is called "users". Whenever I call db.users.findOne({"username":user_query}) I get an error after submitting the form looking like this:

TypeError: Cannot read property 'findOne' of undefined
    at /Users/joshuablew/Desktop/MobiusApp/app.js:86:24
    at Layer.handle [as handle_request] (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/layer.js:95:5)
    at /Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:275:10)
    at /Users/joshuablew/Desktop/MobiusApp/app.js:74:3
    at Layer.handle [as handle_request] (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:317:13)
    at /Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:275:10)
    at /Users/joshuablew/Desktop/MobiusApp/node_modules/connect-flash/lib/flash.js:21:5
    at Layer.handle [as handle_request] (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:317:13)
    at /Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/index.js:275:10)
    at /Users/joshuablew/Desktop/MobiusApp/node_modules/express-validator/lib/express_validator.js:355:5
    at Layer.handle [as handle_request] (/Users/joshuablew/Desktop/MobiusApp/node_modules/express/lib/router/layer.js:95:5)

What can I do differently so that when a user searches a username in the form, the result will be shown on webpage? Thanks.

You should establish the connection to MongoDB first. You can found a guide for example here .

In the findOne function you find the documents where the user name is "user_query". You have to substitute the proper name for proper results.

As an advice, you should use for example user_name instead of user_query for more readable code.

Assuming the db connection is setup successfully, can you check to see if the collection exists via the mongo shell?

A simple "db.users.find()" or "show collections" in the shell will show you whether the users collection exists or not. If it doesn't, you will need to create the collection via a db.createCollection("users")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM