最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot put error in rest api using node js - Stack Overflow

programmeradmin1浏览0评论

Hello I am trying to put the type value in my mongodb database using rest api. However, it shows an error saying cannot put (404 not found).

app.js

app.put('api/types/:_id', function(req,res){
    var id = req.params._id;
    var type = req.body;
    Type.updateType(id, type, {}, function(err, type){
        if(err){
            throw err;
        }
        res.json(type);
    });
});

type.js

module.exports.updateType = function(id, type, options, callback){
    var query = {_id: id};
    var update = {
        name: type.name,
        description: type.description,
        category: type.category
    }

    Task.findOneAndUpdate(query,update, options, callback);
}

Hello I am trying to put the type value in my mongodb database using rest api. However, it shows an error saying cannot put (404 not found).

app.js

app.put('api/types/:_id', function(req,res){
    var id = req.params._id;
    var type = req.body;
    Type.updateType(id, type, {}, function(err, type){
        if(err){
            throw err;
        }
        res.json(type);
    });
});

type.js

module.exports.updateType = function(id, type, options, callback){
    var query = {_id: id};
    var update = {
        name: type.name,
        description: type.description,
        category: type.category
    }

    Task.findOneAndUpdate(query,update, options, callback);
}
Share Improve this question asked May 30, 2017 at 18:50 Ankur ChavdaAnkur Chavda 1736 silver badges17 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

Use '/api/types/:_id' rather than 'api/types/:_id'

One more issue which causes the mentioned error:

Check if there is any typo mistake in the URL which you are trying to access.

In my case, I tried accessing

"/api/users/udpate/:id"

instead of

"/api/users/update/:id"

I have entered udpate instead of update.So express will not be able to find the route which causes this error.

Two solutions can be of this issue:

  1. Can be a typo in endpoints.

    Instead of this

    "uesr/:userId"

    Check for this

    "/user/:userId"

  2. Can be a slash missing in starting.

    Instead of this

    "user/:userId"

    Add this slash in starting

    "/user/:userId"

发布评论

评论列表(0)

  1. 暂无评论