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 'params' of undefined<br> at param (/home/w/node_modules/express/lib/request.js:236:21)<br> at app.put (/home/w/server.js:195:15)<br> at Layer.handle [as handle_request] (/home/w/node_modules/express/lib/router/layer.js:95:5)<br> at next (/home/w/node_modules/express/lib/router/route.js:137:13)<br> at Route.dispatch (/home/w/node_modules/express/lib/router/route.js:112:3)<br> at Layer.handle [as handle_request] (/home/w/node_modules/express/lib/router/layer.js:95:5)<br> at /home/w/node_modules/express/lib/router/index.js:281:22<br> at param (/home/w/node_modules/express/lib/router/index.js:354:14)<br> at param (/home/w/node_modules/express/lib/router/index.js:365:14)<br> 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(!!!)