-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
31 lines (24 loc) · 848 Bytes
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const mongoose = require("mongoose");
const dotenv = require("dotenv");
dotenv.config();
const MONGO_URL = process.env.MONGO_URL;
const mongoDB = async () => {
try {
await mongoose.connect(MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected successfully!");
const fetchedData = await mongoose.connection.db.collection("food");
const data = await fetchedData.find({}).toArray();
const foodCategory = await mongoose.connection.db.collection("category");
const catData = await foodCategory.find({}).toArray();
// Assign the fetched data to the global variables
global.food = data;
global.foodCategory = catData;
console.log(global.food);
} catch (error) {
console.error("Error connecting to MongoDB:", error);
}
};
module.exports = mongoDB;