Advertisement
nubilfi

Unshift array object

Jan 23rd, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // routes/index.js
  2.     io.on('connection', function (socket) {
  3.         Item.find({}, {'item_name':1, 'stock':1}, function(err, docs) {
  4.             if (err) return console.log(err);
  5.  
  6.             // Show latest item
  7.             io.emit('show items', docs);
  8.         });
  9.     });
  10.  
  11. // views/index.hbs
  12. <script src="/socket.io/socket.io.js" type="text/javascript"></script>
  13. <script>
  14.     var socket = io();
  15.     socket.on('show items', function (data) {
  16.         console.log(data);
  17.         var html = '';
  18.         for (var i = 0; i < data.length; i++) {
  19.             html += '<li class="list-group-item" id="listGroup">'+data[i].item_name+'<span class="badge">'+data[i].stock+'</span></li>';
  20.         }
  21.         $('#listItem').html(html);
  22.     });
  23. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement