Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
Request params do not contain proper values

I'm using Mongoose to build a REST API using NodeJs and am running into issues with the params of req.

The code I'm using (model) is as follows:

'use strict';
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var RequestSchema = new Schema({
    query: {
        type: String,
        default: ''
    },
    number: {
        type: String, 
        default: ''
    }, 
    subject: {
        type: String,
        default: ''
    }
});

module.exports = mongoose.model('Cel', RequestSchema)

However, when I use the following code from my controller (answerQuery is used for a POST request) and print out the values I find unexpected values :

exports.answerQuery = function(req, res) {
    console.log('query is : ' + req.params.query); // this is undefined
    console.log('query is : ' + req.body.query); // this is the value of query
    console.log('query is: ' + req.params.number); // this is the value of number
    console.log('subject is : ' + req.params.subject); // this is undefined 
};

I understand why req.body.query works but am confused as to why req.params.query and req.params.subject don't work (return undefined) but req.params.number does. I haven't used Javascript a lot and think that I might be missing something here.

Edit 0: I'm using a POST request for this

Edit 1: This is my route file:

'use strict';

module.exports = function(app) {
    var celRequest = require('../controllers/celSearchController'); 

    // Routes 
    app.route('/celsearch/:number')
        .post(celRequest.answerQuery);
};   
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your route is this:

POST /celsearch/:number

This defines one parameter, number. Parameters are accessed through req.params, which is why req.params.number works.

You are trying to access req.params.subject, referring to a parameter called subject, which doesn't exist. Therefore, it's undefined. Same goes for req.params.query.

Because it's a POST route, and it's most common to pass data to POST routes using the request body, that data ends up in req.body. Since the client is passing a parameter called "query" in the request body, req.body.query works.

So:

  • req.params is used for route parameters, the :NAME placeholders in the route declaration;
  • req.body is used for parameters passed in the request body, commonly used for POST requests;
  • req.query is used for parameters passed in the URL as a query string: /foo/bar?query=somevalue. This is generally used for GET requests.
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!