我正在使用mongdb并拥有一个用户数据库。我想允许用户根据用户名在数据库中搜索其他用户。到目前为止,我已经尝试过并且没有成功实现这一目标。
这是我的前端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>
我在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);
});
我在用于存储用户的数据库中的集合称为&#34;用户&#34;。每当我致电db.users.findOne({"username":user_query})
时,我在提交表格后会收到错误:
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)
我可以采取哪些不同的方式,以便当用户在表单中搜索用户名时,结果会显示在网页上?感谢。
答案 0 :(得分:0)
您应该首先建立与MongoDB的连接。您可以找到例如here的指南。
在findOne
函数中,您可以找到用户名为&#34; user_query&#34;的文档。您必须用正确的名称替换正确的结果。
作为建议,您应该使用user_name
而不是user_query
来获得更易读的代码。
答案 1 :(得分:0)
假设数据库连接设置成功,您可以通过mongo shell检查集合是否存在吗?
一个简单的&#34; db.users.find()&#34;或&#34;显示集合&#34;在shell中将显示用户集合是否存在。如果没有,您将需要通过db.createCollection创建集合(&#34; users&#34;)