Don't GET req.query.name to collection.find in express and MongoDB

828 views Asked by At

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.

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

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

where then I go to get name through

search = req.query.name 

and then insert it into the collection.find({search}.....

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
    });
});

});

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!

UPDATE

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;

from:

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);
}}
});

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);
}}
});

assigning the query req.query.name NO

2

There are 2 answers

4
CherryDT On

It looks like everything is done correctly, except for the actual MongoDB query. {search} expands to {search: search} so it will search inside a field called search in your database and I'm assuming no such field exists.

You can read about the correct MongoDB query syntax here: https://docs.mongodb.com/manual/tutorial/query-documents/

For example, to return only documents where the field email would equal your search query, you'd use {email: search}. I can't give you an exact solution for your case though because you didn't show the structure of your database documents.

0
scofx On

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 !