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

Moved General Discussion
  • I take this opportunity to greet everyone I wanted to expose you a problem:
    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 he doesn't want to know.

    this below is the search.ejs page:

    <!DOCTYPE html>
    <html>
      <head>
        <title>User List</title>
        <link rel='stylesheet' href='/stylesheets/style.css' />
      </head>
      <body>
        <h1>MongoSearch</h1>
    
        <form action="" 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="text here"></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].pid %></li>
             
              <li></li>
    
            
            <%}%>
        </ul>
      </body>
    </html>
    

    advice on how much it would be appreciated ... thanks in advance

  • Hi, while you're welcome to post here, this is not a general forum for Node. It's the community forum for NodeBB, a Node.js based forum software.

    You'll probably have better luck with your question on StackOverflow or Reddit.


Suggested Topics


  • 0 Votes
    5 Posts
    99 Views
  • 0 Votes
    1 Posts
    291 Views
  • 0 Votes
    3 Posts
    808 Views
  • 0 Votes
    4 Posts
    1752 Views
  • 1 Votes
    3 Posts
    990 Views