Guest User

Untitled

a guest
Jan 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. const MongoClient = require('mongodb').MongoClient;
  2. const auth = {
  3. user: process.env.user,
  4. password: process.env.password
  5. };
  6. let db = null;
  7. const loadDB = async () => {
  8. if (db) {
  9. return db;
  10. }
  11. const client = await MongoClient.connect(
  12. process.env.url,
  13. {
  14. auth: auth
  15. }
  16. );
  17. db = client.db('tacos');
  18. return db;
  19. };
  20. module.exports = async function(context, req) {
  21. try {
  22. const database = await loadDB();
  23. let recipes = await database
  24. .collection('Recipes')
  25. .find()
  26. .toArray();
  27. context.res = {
  28. body: { items: recipes }
  29. };
  30. } catch (error) {
  31. context.log(`Error code: ${error.code} message: ${error.message}`);
  32. context.res = {
  33. status: 500,
  34. body: { message: 'An error has occured, please try again later' }
  35. };
  36. }
  37. };
Add Comment
Please, Sign In to add comment