-2
{
    "_id": {
        "$oid": "5a2de0a00d6baa43e8b925d0"
    },
    "name": "test",
    "playList": [
        {
            "url": "https://p.scdn.co/mp3-preview/8aa799e60164f8a1fb311188d9d85ef65d7782c6?cid=ed36a056ee504173a3889b2e55cbd461",
            "artist": "Kenny G",
            "songName": "My Heart Will Go On (Love Theme from \"Titanic\")",
            "_id": {
                "$oid": "5a2de0ad0d6baa43e8b925d1"
            }
        },
        {
            "url": "https://p.scdn.co/mp3-preview/7c49854f18e6dfda6cd97ab5e8bc139d7ca82b7c?cid=ed36a056ee504173a3889b2e55cbd461",
            "artist": "PRODUCE 101",
            "songName": "PICK ME",
            "_id": {
                "$oid": "5a2de13b0d6baa43e8b925d2"
            }
        }
    ],
    "__v": 0
}

I have a database called channels where each channels contain a playList as shown below. I want to delete a single item when a button is clicked. I can handle the onClick event part, but I am not sure how to implement the routes part.

I know that I start by doing something like

router.delete(''/channels/:id', function(req, res){

    something here...

})

but how can I access a particular item (probably with a unique id?) and delete it from the DB?

EDIT

By using the GET below

router.get('/channels/:id',
        isLoggedIn,
        function(req, res) {
            channel.findOne({'name':req.params.id},function(err,channeldata){
                if(err || channeldata === null){
                    res.status(404).send({
                        message: 'Channel Not Found',
                        data: []
                    })
                }
                else {
                    res.status(200).json({
                        message: "channel to "+req.params.id+"success",
                        data:channeldata
                    })
                }
            })
    });

I get the data for a single channel in my DB.

But since I am new to this stuff, I am not sure how to access each item of the playList and delete a single data.

EDIT2

var mongoose = require('mongoose');

var ChannelSchema = new mongoose.Schema({
    name: {type:String,required:true},
    playList: [{
        songName: { type : String },
        artist: { type : String },
        url: { type : String }
    }]
})

module.exports = mongoose.model('Channel',ChannelSchema);
Dawn17
  • 7,825
  • 16
  • 57
  • 118

1 Answers1

1

You can try the following snippet that contains the DELETE (part of CRUD) endpoint for your resource collection (i.e. the channels):

router.delete('/channels/playlist/song', isLoggedIn, (req, res) => {  

  const channel_id = req.query.channelId;
  const song_id    = req.query.songId;

  // the following query deletes a song form a playlist of a certain channel
  channel.update({_id: ObjectId(channel_id)},{$pull:{playList:{_id:ObjectId(song_id)}}})
    .exec()
    .then(result => {

      // for checking if document was found and deleted
      // mongodb actually returns special object `result`
      //   which has its own certain fields
      res.status(200).send({
        status: "success",
        message: result
      });   

    })
    .catch(error => {
        // here we see if we had any problem with server or db itself
        console.log(error)
        res.status(500).send({
        success: false,
        message: "Something went wrong with DELETE /channels/:id"
      })
    })
});

I assume that you know what ObjectId() function does if you do not have it declared, declare the following comment in the beginning of the file (where you require everything)

const mongoose = require('mongoose'); // you must have this
const ObjectId = mongoose.Types.ObjectId; // gets the function

Let me know if this helps, or if you do not understand something - I will make an edit so that you get it.

oneturkmen
  • 1,288
  • 1
  • 11
  • 27