0

I am trying to write a function similar to this one documented here (Using the PUT method with Express.js), but am failing.

In this example, I need to retrieve the value in :company What I am getting is undefined

I tried many variations including

var company = req.company;
var company = JSON.stringify(req.company);
var company = req.company[0];

Thus far, all have yielded the same result. What is the correct syntax?

app.put('/api/:company', function (req, res) {
    var company = req.company;

    company = _.extend(company, req.body);

    company.save(function(err) {
    if (err) {
        return res.send('/company', {
            errors: err.errors,
            company: company
        });
    } else {
        res.jsonp(company);
    }

}); 

UPDATE: Per the suggestion of @Robot, I have also tried

var company = req.param.company 

and 

console.log(req.param)

the second is the closest i have gotten to an answer with

[function param]

Additional testing: let foo = req.param; then foo() yields:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>TypeError: Cannot read property &#39;params&#39; of undefined<br> &nbsp; &nbsp;at param (/home/w/node_modules/express/lib/request.js:236:21)<br> &nbsp; &nbsp;at app.put (/home/w/server.js:195:15)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/home/w/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at next (/home/w/node_modules/express/lib/router/route.js:137:13)<br> &nbsp; &nbsp;at Route.dispatch (/home/w/node_modules/express/lib/router/route.js:112:3)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/home/w/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at /home/w/node_modules/express/lib/router/index.js:281:22<br> &nbsp; &nbsp;at param (/home/w/node_modules/express/lib/router/index.js:354:14)<br> &nbsp; &nbsp;at param (/home/w/node_modules/express/lib/router/index.js:365:14)<br> &nbsp; &nbsp;at Function.process_params (/home/w/node_modules/express/lib/router/index.js:410:3)</pre>
</body>
</html>

FIX: Thanks to @AmIDeranged and this tutorial https://www.robinwieruch.de/node-express-server-rest-api

you can use either

req.params.company

or

${req.params.company}

I am not certain what the ${} adds to the conversation, but that is left for another day(!!!)

user3877654
  • 1,045
  • 1
  • 16
  • 40

2 Answers2

0
app.put('/api/:company', handler);

sending request to /api/abcCompany, req.param.company would return abcCompany

noting will be in req.company unless you write your own middleware which can fill the req.company to pass it to other middlewares

you have access to company data in req.param.company

Robot
  • 913
  • 6
  • 8
  • Thank you for your reply. I am still getting something wrong. since the route (represented in :company) is a string, I am trying to print it using console.log(company) => undefined and console.log(req.params) =>[Function: param]. What am I misunderstanding? – user3877654 Apr 20 '21 at 16:39
0

Thanks to @AmIDeranged and this tutorial https://www.robinwieruch.de/node-express-server-rest-api

you can use either

req.params.company

or

${req.params.company}
user3877654
  • 1,045
  • 1
  • 16
  • 40