Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get req.query.name to collection.find #335

Closed
onoff0 opened this issue Mar 7, 2021 · 2 comments
Closed

Can't get req.query.name to collection.find #335

onoff0 opened this issue Mar 7, 2021 · 2 comments

Comments

@onoff0
Copy link

onoff0 commented Mar 7, 2021

Hi, I can't get req.query.name to pass in the collection.find I expose the problem below:

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 doesn't want to know, do you have any ideas about it?

@mathieudutour
Copy link
Collaborator

req.query.name is a string or a string array while 5044 is a number. parseInt(req.query.name) should work. Please use stackoverflow for questions like that

@onoff0
Copy link
Author

onoff0 commented Mar 9, 2021

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants