0

This is the url of api.

http://localhost:3000/verify?id=55

And this is the snipet for getting the query value.

server.get('/verify', async (req,res,next) => {
  try { console.log(req.query.id); }
  catch{err}
});

As the log prints out "undefined" , is there a problem on url expression?

3
  • You need a query-parser middleware to access the query like that. Are you using express? Commented May 3, 2021 at 16:07
  • I see. no. it is Restify. Commented May 3, 2021 at 16:08
  • 2
    Do you have server.use(restify.queryParser());? Commented May 3, 2021 at 16:11

2 Answers 2

1

Try like this once

server.get('/verify', async (req,res,next) => {
try { 
  var id = JSON.parse(req.query.id); 
}
  catch{err}
});
Sign up to request clarification or add additional context in comments.

Comments

0

Your code should look something like this (note the use of query parser middleware):

const restify = require('restify');

const server = restify.createServer({
  name: 'myapp',
  version: '1.0.0'
});

server.use(restify.plugins.bodyParser());
server.use(restify.plugins.queryParser());

server.get('/verify', async (req, res, next) => {
  console.log(req.query.id);
});

2 Comments

Tried those middleware but now got the following error. {"code":"Unauthorized","message":"caused by TypeError: req.query is not a function"}.
It worked after updating npm packages. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.