Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VisualStudio代码文本编辑器在mongodb(node.js)中实现搜索功能_Node.js_Mongodb_Visual Studio Code_Full Text Search_Partial - Fatal编程技术网

使用VisualStudio代码文本编辑器在mongodb(node.js)中实现搜索功能

使用VisualStudio代码文本编辑器在mongodb(node.js)中实现搜索功能,node.js,mongodb,visual-studio-code,full-text-search,partial,Node.js,Mongodb,Visual Studio Code,Full Text Search,Partial,我在前端(userlist.ejs)中完成了搜索功能,但我不知道如何在mongodb中执行搜索功能,也不知道如何从mongodb获取数据,但我将文档从mongodb(index.js,变量为“userlist”)传递到前端(userlist.ejs)。我在下面附上我的代码,请找到它并解决我的问题 在这里,我附上app.js的代码 var createError = require('http-errors'); var express = require('express'); var path

我在前端(userlist.ejs)中完成了搜索功能,但我不知道如何在mongodb中执行搜索功能,也不知道如何从mongodb获取数据,但我将文档从mongodb(index.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, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, 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>
[
  {
    "_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",
    "Status": "Admin Approved"
  },
  {
    "_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.",
    "Category": "Programming",
    "Sub-category": "Learning",
    "Status": "Admin Approved"
  },
  {
    "_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.",
    "Category": "Programming",
    "Sub-category": "Learning",
    "Status": "Admin Approved"
  },
  {
    "_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. ",
    "Category": "Programming",
    "Sub-category": "Learning",
    "Status": "Admin Approved"
  }
]