使用Visual Studio代码文本编辑器在mongodbnode.js中实现搜索功能

问题描述

我完成了前端(userlist.ejs)的搜索功能,但是我不知道如何在mongodb中执行搜索功能,也不知道如何从mongodb中获取数据,但是我从mongodb传递了文档(索引。 js,其变量为“ userlist”)到前端(userlist.ejs)。我在下面附加了我的代码,请务必找到它并解决我的问题。

在这里,我附上我的app.js代码

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

// New Code
var monk = require('monk');
var db = monk('localhost:27017/nodetest1');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views',path.join(__dirname,'views'));
app.set('view engine','ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname,'public')));

// Make our db accessible to our router
app.use(function(req,res,next){
  req.db = db;
  next();
});

app.use('/',indexRouter);
app.use('/users',usersRouter);

// catch 404 and forward to error handler
app.use(function(req,next) {
  next(createError(404));
});

// error handler
app.use(function(err,req,next) {
  // set locals,only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

这是我的index.js文件

var express = require('express');
var router = express.Router();

/* GET Userlist page. */
router.get('/userlist',function(req,res) {
  var db = req.db;
  var collection = db.get('cust');
  collection.find({},{},function(e,docs){
      res.render('userlist',{
          "userlist" : docs
      });
  });
})
module.exports = router;

这是我的userlist.ejs文件

<!DOCTYPE html>
<html>
<head>
<Meta name="viewport" content="width=device-width,initial-scale=1">
<style>
  h2 {text-align: center;}
* {
  Box-sizing: border-Box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th,#myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header,#myTable tr:hover {
  background-color: #f1f1f1;
}
</style>
</head>
<body>

<form style="margin:auto;max-width:300px">
  <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for learning.." title="Type in a name" >
</form>

<table id="myTable" style="margin:auto;max-width:300px">
  <tr>
    <td>GK for Kids</td>
  </tr>
  <tr>
    <td>Python for Beginners</td>
  </tr>
  <tr>
    <td>Java for Beginners</td>
  </tr>
  <tr>
    <td>C for Beginners</td>
  </tr>
  <tr>
    <td>C++ for Beginners</td>
  </tr>
</table>

<script>
function myFunction() {
  var input,filter,table,tr,td,i,txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toupperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toupperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }       
  }
}
</script>
</body>
</html>

这是我的mongodb数据

[
  {
    "_id": "5fa3a4be0150aca7e56a7040","Creator": "admin@leap.com","Tags": "Kids general KNowledge","Title": "GK for Kids","Owner": "admin@leap.com","Description": "GK for kids course covers MCQ","Category": "Competitive exam","Sub-category": "General KNowledge","Status": "Admin Approved"
  },{
    "_id": "5fa3ad470150aca7e56a7041","Creator": "admin1@leap.com","Tags": "Python Basics","Title": "Python for Beginners","Owner": "admin1@leap.com","Description": "Python is an interpreted,object-oriented,high-level programming language with dynamic semantics. Its high-level built in data structures,combined with dynamic typing and dynamic binding,make it very attractive for Rapid Application Development,as well as for use as a scripting or glue language to connect existing components together.","Category": "Programming","Sub-category": "Learning",{
    "_id": "5fa3ade30150aca7e56a7042","Creator": "admin2@leap.com","Tags": "Java Basics","Title": "Java for Beginners","Owner": "admin2@leap.com","Description": "Java is a high-level programming language developed by Sun Microsystems. It was originally designed for developing programs for set-top Boxes and handheld devices,but later became a popular choice for creating web applications.",{
    "_id": "5fa3ea44d82da320c25919c1","Creator": "admin3@leap.com","Tags": "C Basics","Title": "C for Beginners","Owner": "admin3@leap.com","Description": "C is a general-purpose,high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs.",{
    "_id": "5fa3ea9bd82da320c25919c2","Creator": "admin4@leap.com","Tags": "C++ Basics","Title": "C++ for Beginners","Owner": "admin4@leap.com","Description": "C++ is a cross-platform language that can be used to create high-performance applications. C++ was developed by Bjarne Stroustrup,as an extension to the C language. ","Status": "Admin Approved"
  }
]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)