SHARE
TWEET

NodeJS SHOP Project

MilanR May 23rd, 2019 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var router = express.Router();
  3. var MongoClient = require('mongodb').MongoClient;
  4. var ObjectId = require('mongodb').ObjectId;
  5.  
  6.  
  7. var url = 'mongodb://localhost:27017/shop';
  8. router.get('/', function (req, res) {
  9.     MongoClient.connect(url,{ useNewUrlParser: true }, function(err, client)
  10.     {
  11.         var data = [];
  12.         var db = client.db('shop');
  13.         ucitajSveIzOrders(db, function(err, dataIzOrders) {
  14.            dataIzOrders.forEach(function (element, indeks) {
  15.                kreirajBlok(db, element, function (blok) {
  16.                   data.push(blok);
  17.                });
  18.            });
  19.            console.log(data);// RADI SUPER
  20.         });
  21.  
  22.     });
  23. });
  24.  
  25. function ucitajSveIzOrders(db, callback) {
  26.     var collection = db.collection('orders');
  27.     collection.find().toArray(function(err, doc)  {
  28.         callback (err, doc);
  29.     });
  30. }
  31.  
  32. function kreirajBlok(db, element, callback) {
  33.     var blok = {};
  34.  
  35.     blok['username'] = element.username;
  36.     blok['date'] = element.date;
  37.     blok['astal'] = 1;
  38.  
  39.  
  40.     var orders = [];
  41.     var cene = []
  42.  
  43.     // {id: '', count:3}
  44.     var arrOrders = element.order;
  45.     arrOrders.forEach(function (order) {
  46.         ucitajInformacijeIzBaze(db, order, function (dopunjenNaziv, ukupnaCena) {
  47.             orders.push(dopunjenNaziv);
  48.             cene.push(ukupnaCena);
  49.         });
  50.     });
  51.    
  52.     //TODO OVO NA DOLE TREBA DA SE IZVRSI KAD SE IZVRSI OVO GORE
  53.     var total = 0;
  54.     cene.forEach(function (element) {
  55.         total += element;
  56.     });
  57.  
  58.     blok['orders'] = orders;
  59.     blok['cene'] = cene;
  60.  
  61.     blok['total'] = total;
  62.     callback(blok);
  63. }
  64.  
  65. function ucitajInformacijeIzBaze(db, order, callback) {
  66.     var collection = db.collection('menu');
  67.     var o_id = new ObjectId(order.id);
  68.     collection.findOne({_id:o_id}, function(err, doc)  {
  69.         callback (order.count+"x "+doc.name, doc.cena*order.count);
  70.     });
  71. }
  72. module.exports = router;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top