PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 18, 2022

[FIXED] How can I fetch req.param and get body in Node.js?

 July 18, 2022     document-body, express, node.js, parsing     No comments   

Issue

I found this example to get body, but how can I retrive the req.param variable? I do not have req in this case?!

app.post('/:tableName', function(res){

    var data = '';

    res.on('data', function (chunk){
        data += chunk;
    });

    res.on('end',function(){

        var obj = JSON.parse(data);

        var schema = require('./schema/' + req.param('tableName'));
        var record = new schema(obj);

        record.save(function(err) {
        if (err) {
            console.log(err);
            res.status(500).json({status: 'failure'});
        } else {
            res.json({status: 'success'});
        }
    });
    })

});

UPDATE

I modified the method signature like this, but then first res.on will not get called.

app.post('/:tableName', function(req, res) {

Solution

Your callback should take in req and res, but is currently only taking in req as res.

app.post('/:tableName', function(req, res){ should give the function what it expects.



Answered By - max
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing