Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getAllStock = (req, res) => {
  2.  
  3.     const urlObj = url.parse(req.url, true);
  4.     const query = urlObj.query;
  5.     if (query.name == null) {
  6.         res.status(200).end('no bar with such name')
  7.     }
  8.     else {
  9.  
  10.         mongoose.connect(mongourl, options)
  11.             .then(async () => {
  12.                 let result = await bar.find({name:query.name}, (err) => {
  13.                     if (err)
  14.                         throw err;
  15.                 });
  16.  
  17.                 if (result) {
  18.  
  19.                     let tempDrinkArr = []
  20.                     console.log('this is the stock:\n\n' + result);
  21.                     console.log('this is the name:\n' + result.name);
  22.                     for (let i = 0; i < result.stock.length; i++) {
  23.                         tempDrinkArr.push(`${result.stock[i].bottles} bottles of ${result.stock[i].name} in stock`);
  24.                     }
  25.  
  26.                     res.writeHead(200);
  27.                     res.end(tempDrinkArr.toString());
  28.                 }
  29.                 else {
  30.                     res.end('no drinks in stock');
  31.                 }
  32.             })
  33.             .catch(err => {
  34.                 console.error('some error occurred', err)
  35.                 res.end(err.message);
  36.             })
  37.     }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement