Express REST API - Delete Method -


i getting stuck on delete method api. application requires user log in , can add courses. courses stored in nested array inside user model. want user able cancel (delete) course view , have course deleted user's profile on server. getting 404 response event though variables comparing identical.

this ajax call delete specific course:

jquery.ajax({     url: "/test/signups/5387c1a0fb06e48f4658170c",      type: "delete",      success: function (data, textstatus, jqxhr) {        console.log("post resposne:");        console.dir(data);        console.log(textstatus);        console.dir(jqxhr);      }   }); 

this delete method:

app.delete('/test/signups/:id', isloggedin, function(req, res) {     user.findone({'_id': req.user.id }, function(err, user) {         if (err)            return done(err);      if (user) {         var found = false;         var singlesignup = user.signup.filter(function(e){ return e._id == req.params.id })[0]          user.signup.foreach(function (singlesignup, index) {             if (singlesignup._id === req.params.id) {                 found = index;             }         });          if(found) {             user.signup.splice(found, 1);             res.json(200, {status: 'deleted'});         } else {             res.json(404, {status: 'invalid survey question deletion'});         }     }           }); }); 

the _id values in mongodb not strings, instances of objectid class , don't work correctly == or === operators. it's nuisance. anyway try converting them strings before comparing: singlesignup._id.tostring() === req.params.id. correct in long run, make sure handle cases of null, string or objectid. consider helper library such objectid.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -