dimanche 24 mars 2019

How to fix Uncaught TypeError: Cannot read property 'id' of null in node?

I am trying to create a web page application using node, ember and mongodb which is able to edit or to delete the existing songs on my database, the webpage is already able to display the songs and to add new ones. The problem occurs when I click on my "EDIT" link next to a song - it throws "Uncaught TypeError: Cannot read property 'id' of null" when it's supposed to fetch me a song by it's id.

Here's my app/routes.js code:

...
router.route('/songs/:song_id')
                            .put(function(req, res) { songs.updateSong(req, res, req.params.song_id) })
                            .delete(function(req, res) { songs.deleteSong(req, res, req.params.song_id) });
...

Here's my api/song.js code:

...
module.exports.updateSong = function(req, res, id) {
        Song.findByIdAndUpdate(id, {$set: req.body.song}, function(err, song) {
            if (err) {
                res.send(err);
            };
            res.json({song: song});
        });
};

module.exports.deleteSong = function(req, res, id) {
        Song.findByIdAndRemove(id, function(err, song) {
           if (err) {
                res.send(err);
           }
            res.json({song: song});
        });
};
...

Here's my app/router.js code:

...
Router.map(function() {
  this.route('song');
  this.route('about');
  this.route('new');
  this.route('edit', {
      path: ":song_id"
  });
});
...




Aucun commentaire:

Enregistrer un commentaire