不要在express和MongoDB中将req.query.name获取到collection.find [英] Don't GET req.query.name to collection.find in express and MongoDB

查看:71
本文介绍了不要在express和MongoDB中将req.query.name获取到collection.find的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mongodb数据库,在该数据库中有其内部日志数据.使用Express,Node和Monk(遵循指南)我必须尝试从搜索文本框中获取数据,停止了如何请求此数据的操作.

I use a mongodb database where I have its internal log data. With Express, Node and Monk (following this guide) I have to try to get data from a search text box, and I stopped on how to request this data.

在userlist.ejs页面中,我实现了一个发送GET请求的表单:

from the userlist.ejs page I have implemented a form where I send a GET request:

http://localhost:3000/userlist?name=5044

那我去哪里找名字

search = req.query.name 

,然后将其插入 collection.find({search} .....

下面是index.js:

below is the index.js:

/* GET Userlist page. */
router.get('/userlist', function(req, res) {

    search = req.query.name;

    var db = req.db;
    var collection = db.get('startup_log');
    collection.find({search},{},function(e,docs){
        res.render('userlist', {
          "userlist" : docs
    });
});

});

这是我发送请求的userlist.ejs页面.从index.ejs处理完以后,稍后它应该在for循环中显示它,

this is the userlist.ejs page where I send the request. which later should display it through the for loop after processing it from index.ejs,

 <!DOCTYPE html>
<html>
   <head>
     <title>User List</title>
     <link rel='stylesheet' href='/stylesheets/style.css' />
   </head>
 <body>
     <h1>User List</h1>

     <input type="text" id="titles" />

<form action="/userlist" method="GET" encType="multipart/form-data">
  <label htmlFor="newNote">New Note:</label>
    <input type="text" name="name"  className="form-control" rows="5" 
     id="name" placeholder="5044"></input>
    <button id="done" type="submit" className="btn btn-primary pull- 
     right">Done</button>
    <button id="cancel" className="btn btn-warning pull- 
   right">Cancel</button>
</form>

 <ul>
    <% for (var i = 0; i < userlist.length; i++) {%>
 
    <li><%= userlist[i].hostname %></li>
     
        <li></li>        
     <%}%>
  </ul>
 </body>
</html>

我不知道如何传递给集合.找到我在文本框中输入的查询!由于无法找到有关我的案件的具体信息,我们将不胜感激.预先感谢!

i can't figure out how to pass to collection.find the query i enter in the text box! any help would be appreciated as I cannot find specific information about my case. Thanks in advance!

更新

我可以说我已经尝试了所有方法,我将放弃!我也尝试过使用if if else,我尝试过更改字段,但是什么也没做,这并没有该死的

I can say that I have tried them all, I am about to give up! I also tried with an if else, I tried to change the field, but nothing, it does not take the damn

let query = req.query.name;

来自:

http://localhost:3000/search?name=5044

有趣的是,它以这种方式可以很好地工作:

the funny thing is that it works perfectly well in this way:

router.get('/search', async (req, res)=> {

if (!req.query.name)
return res.render("search", {
  title: "NodeMongo Search",
  userlist:[],
});

else {
  const url = 'localhost:27017/local'; // Connection URL
  const db = require('monk')(url);
  const collection = db.get('startup_log')
  try{
    
let query = 5044; //assigning the pid directly to the variable query
const userlist = await collection.find({pid:query});
res.render("search",{userlist});
  }catch(e){
  console.log(e);
}}
});

因此将5044分配给查询变量可以很好地运行,但是:

so assigning 5044 to the query variable run perfectly, but:

router.get('/search', async (req, res)=> {

if (!req.query.name)
return res.render("search", {
  title: "NodeMongo Search",
  userlist:[],
});

else {
  const url = 'localhost:27017/local'; // Connection URL
  const db = require('monk')(url);
  const collection = db.get('startup_log')
  try{
    
let query = req.query.name;
const userlist = await collection.find({pid:query});
res.render("search",{userlist});
  }catch(e){
  console.log(e);
}}
});

分配查询 req.query.name

推荐答案

对于那些对解决方案感兴趣的人来说, req.query 仅传递字符串,所以我以这种方式解决了这个问题;

For those interested in the solution, it is that req.query only passes strings, so I solved it in this way;

var query = (+req.query.name);

其他想法将被很好地接受!

Other ideas would be well accepted !

这篇关于不要在express和MongoDB中将req.query.name获取到collection.find的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆