Using the PUT method with Express.js

前端 未结 4 1595
轮回少年
轮回少年 2021-01-01 15:02

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 every

4条回答
  •  我在风中等你
    2021-01-01 15:28

    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 express, you could write something like:

    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.

提交回复
热议问题