0

Im trying to update my REST API using _id attribute but nothing happens if I try to use unique _id. It works if I update my entry uisng parametr like room name or room number. Could somebody help me please? Where do I have a mistake? It makes me just crazy!

app.put('/api/conferenceRooms/:_id&:_name', (req, res) => {
    var id = req.params._id;
    var name = req.params._name;
    var conferenceRoom = req.body;
    ConferenceRoom.updateConferenceRoom(id, name, conferenceRoom, {}, (err, conferenceRoom) => {
        if(err){
            throw err;
        }
        res.json(conferenceRoom);
    });
});


// Update ConferenceRoom
module.exports.updateConferenceRoom = (id, name, conferenceRoom, options, callback) => {
    var query = {_id: id, name:name};
    var update = {
        temperature: conferenceRoom.temperature
    }
    ConferenceRoom.findOneAndUpdate(query, update, options, callback);
}

I checked everything already 1000 times

3 Answers 3

1

I think it is because you are trying to query _id with a string of the id, but it is likely type ObjectId in MongoDB (default I believe). Try something like:

var ObjectId = require('mongoose').Types.ObjectId; 
var query = { _id: new ObjectId(id) };
Sign up to request clarification or add additional context in comments.

1 Comment

It doesnt see my id. In ngrok I can see PUT /api/conferenceRooms/&:_IoT 404 Not Found when I send 349349d1.ngrok.io/api/conferenceRooms/… Headers Content-Type application/json and I add raw body { "temperature" : 4 }
0

Your name is probably undefined or wrong.

change

'/api/conferenceRooms/:_id&:_name'

To

app.put('/api/conferenceRooms/:_id/:_name', (req, res) => {


}

Comments

0

You would have the change Url. As now in

'/api/conferenceRooms/:_id&:_name'

:_id is a Reqest URL Parameter and :_name is Request Query Parameter

What you can do is that you could change both to either Reqest URL or Query Parameter.

When accessing URL Parameter - req.params._id

When accessing Query Parameter - req.query._id

1 Comment

if I access req.parms_id in ngrok I see PUT /api/conferenceRooms/&:_IoT 404 Not Found when using req_query._id the same :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.