2

I can't seem to wrap my head around how to submit front end (express) data through socket to mongodb. I have one simple database with one collection called 'playlist' and two tables called 'youtube_id' and 'upvotes'

The following is in my app.js

 io.on('connection', function(socket){
    socket.on('insertSong', function(song, upvote){
            mongo.connect(MONGO_URL, function(err, db) 
                {
                var playlist = db.collection('playlist');
                playlist.insert({youtube_id: song, upvotes: upvote});

                });
            });


  });

and here is the code in my index.html

    <script>
                $(function(){
                        var playlist = io();
                        var song = "PEGccV-NOm8";
                        var upvote = 1;
                        playlist.emit('playlist', {song,upvote});


                });
    </script>
2
  • Did you successfully send a message over socket yet? Or is that whats failing? Commented Feb 8, 2018 at 16:49
  • I used the tutorial to make a simple chat program work with socket so sockets is working & installed and mongodb is also up & running. I don't receive any errors and nothing is added to the database when i run the frontend. Commented Feb 8, 2018 at 16:50

1 Answer 1

2

You are emiting a 'playlist' event in your client and waiting for a 'insertSong' on your server.

Sign up to request clarification or add additional context in comments.

Comments

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.