Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda