Sunday, 23 April 2017

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.



via Ron7

No comments:

Post a Comment