Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
180
rated 0 times [  185] [ 5]  / answers: 1 / hits: 132139  / 11 Years ago, tue, september 3, 2013, 12:00:00

I'm trying to implement update functionality to an Express.js app, and I'd like to use a PUT request to send the new data, but I keep getting errors using PUT. From everything I've read, it's just a matter of using app.put, but that isn't working. I've got the following in my routes file:



send = function(req, res) { 
req.send(res.locals.content);
};

app.put('/api/:company', function(res,req) {
res.send('this is an update');
}, send);


When I use postman to make a PUT request, I get a cannot PUT /api/petshop as an error. I don't understand why I can't PUT, or what's going wrong.


More From » node.js

 Answers
9

You may be lacking the actual update function. You have the put path returning the result back to the client but missing the part when you tell the database to update the data.


If you're using MongoDB and ExpressJS, you could write something like this :


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);
}
})
});

This mean stack project may help you as it covers this CRUD functionality which I just used here swapping their articles for your companies. same same.


[#75916] Tuesday, September 3, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileenreynap

Total Points: 140
Total Questions: 106
Total Answers: 99

Location: Andorra
Member since Sun, Oct 18, 2020
4 Years ago
;