-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (30 loc) · 890 Bytes
/
index.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
32
33
34
const mongoose = require("mongoose");
const initData = require("./data.js");
const Listing = require("../models/listing.js");
const mongo_url = "mongodb://127.0.0.1:27017/wanderlust";
main()
.then(() => {
console.log("Connected to the database");
initDB();
})
.catch((err) => console.log(err));
async function main() {
await mongoose.connect(mongo_url);
}
// To delete previous data and insert new data
async function initDB() {
try {
await Listing.deleteMany({});
initData.data = initData.data.map((obj) => ({
...obj,
owner: "662e4b9936ca3cf5c1035b6e",
})),
await Listing.insertMany(initData.data);
console.log("Data has been initialized");
} catch (error) {
console.error("Error initializing data:", error);
} finally {
// Close the MongoDB connection after initializing data
await mongoose.connection.close();
}
}