简体   繁体   中英

Sending query params as request from Postman

I'm sending a get request from postman to this url /api/v1/get_services/p but I cannot get the query parameters. The result I get is [object object].

My postman get request looks like this:

Postman request: localhost:4000/api/v1/get_services/p?category=Music



Express route:
     apiRoutes.get('/api/v1/get_services/p', async (req, res) => {
      const { category } = req.query;   
      const rootRef = firebase.database().ref(`${category}`);
      await rootRef.once('value', (snap) => {
        if (snap.exists()) {
          console.log(snap.val());
          res.status(200);
          res.send(snap.val());
        } else {
          res.status(404);
          res.send('No services found');
        }
      });
    });

Is there any additional configuration that I should add to express middleware?

Write a object builder function to take the correct req.query sub elements from the parameter ?

function parameterBuilder (body) {

  var record = {}
  record.category = body.category | ' ';
  record.name  = body.name | ' ';
  return record;

}

 apiRoutes.get('/api/v1/get_services/p', async (req, res) => {
  const { category } = req.query; 
     //New code
    var parameters = parameterBuilder(req.query);
    console.log(parameters) // check to see all ur parameters is there
    //end of new code
      const rootRef = firebase.database().ref(`${category}`);
      await rootRef.once('value', (snap) => {
        if (snap.exists()) {
          console.log(snap.val());
          res.status(200);
          res.send(snap.val());
        } else {
          res.status(404);
          res.send('No services found');
        }
      });
    });

您的要求应为:

localhost:4000/api/v1/get_services/Music

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM