Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var MongoClient = require('mongodb').MongoClient
  2. var url = 'mongodb://localhost:27017/myproject';
  3. function output (user, orders) {
  4.     // output data in pretty format
  5. }
  6.  
  7. function logError (error) {
  8.     // Log the error (could be console, a service, a file etc)
  9. }
  10.  
  11. // Connect to mongo server
  12. MongoClient.connect(url, (err, db) => {
  13.     const userId = process.argv[2];
  14.     db.collection('access_log').insert({ userId, date: new Date() }, (err, result) => {
  15.         if (err) {
  16.             logError(err);
  17.         } else {
  18.             db.collection('users').find({ userId }).toArray((err, users) => {
  19.                 if (err) {
  20.                     logError(err);
  21.                 } else {
  22.                     db.collection('orders').find({ userId }).toArray((err, orders) => {
  23.                         if (err) {
  24.                             logError(err);
  25.                         } else {
  26.                             output(users[0], orders);
  27.                         }
  28.                     });
  29.                 }
  30.             });
  31.         }
  32.     });
  33. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement