I can’t connect to MongoDB database, yet I tried everything ! I have successfully replaced the password.
JavaScript
x
13
13
1
const mongoose = require("mongoose");
2
3
mongoose
4
.connect(
5
"mongodb+srv://vibess:0KksWIBp6slcBLm0@cluster0.iuvoi.mongodb.net/?retryWrites=true&w=majority",
6
{
7
useNewUrlParser: true,
8
useUnifiedTopology: true,
9
useCreateIndex: true,
10
}
11
)
12
.then(() => console.log("Connected !!"))
13
.catch(() => console.log("Not connected!"));
Here is the Database
Advertisement
Answer
You will need another function to complete the operation. the function is usually called run then you need to write all the operations of your server in the scope of this function. here is an example of a server of mine Also you need to declare the name of your database before text retrywrites=true
JavaScript
1
175
175
1
//connect to mongodb
2
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.qtoag.mongodb.net/Teletale?retryWrites=true&w=majority`;
3
4
const client = new MongoClient(uri, {
5
useNewUrlParser: true,
6
useUnifiedTopology: true,
7
});
8
9
async function run() {
10
try {
11
await client.connect((err) => {
12
const db = client.db("Teletale");
13
const djiPackages = db.collection("Devices");
14
const bookingsCollection = db.collection("bookings");
15
const testimonialCollection = db.collection("testimonials");
16
const usersCollection = db.collection("users");
17
18
// ==============GET API ====================
19
//GET API
20
app.get("/", (req, res) => {
21
res.send("Welcome to Teletale");
22
});
23
24
//GET API (dji Package)
25
app.get("/Devices", async (req, res) => {
26
const result = await djiPackages.find({}).toArray();
27
res.send(result);
28
});
29
30
//GET API (users)
31
app.get("/users", async (req, res) => {
32
const result = await usersCollection.find({}).toArray();
33
res.send(result);
34
});
35
36
// verify admin data form database
37
app.get("/users/:email", async (req, res) => {
38
const email = req.params.email;
39
const query = { email: email };
40
const user = await usersCollection.findOne(query);
41
let isAdmin = false;
42
if (user?.role === "admin") {
43
isAdmin = true;
44
}
45
// localhost:5000/users/admin@admin.com will show true
46
res.json({ admin: isAdmin });
47
});
48
49
//GET API (Bookings)
50
app.get("/bookings", async (req, res) => {
51
let query = {};
52
const email = req.query.email;
53
if (email) {
54
query = { email: email };
55
}
56
const result = await bookingsCollection.find(query).toArray();
57
res.send(result);
58
});
59
60
//GET Dynamic (Bookings)
61
app.get("/bookings/:id", async (req, res) => {
62
const id = req.params.id;
63
const query = { _id: ObjectId(id) };
64
const result = await bookingsCollection.findOne(query);
65
res.send(result);
66
});
67
68
//GET Dynamic (products)
69
app.get("/Devices/:id", async (req, res) => {
70
const id = req.params.id;
71
const query = { _id: ObjectId(id) };
72
const result = await djiPackages.findOne(query);
73
res.send(result);
74
});
75
76
//GET (testimonials)
77
app.get("/testimonials", async (req, res) => {
78
const result = await testimonialCollection.find({}).toArray();
79
res.send(result);
80
});
81
82
// ==========================POST API=========================
83
//POST API (dji Package)
84
app.post("/Devices", async (req, res) => {
85
const newTours = req.body;
86
const result = await djiPackages.insertOne(newTours);
87
res.send(result);
88
});
89
90
//POST API (users)
91
app.post("/users", async (req, res) => {
92
const user = req.body;
93
const result = await usersCollection.insertOne(user);
94
console.log(result);
95
res.send(result);
96
});
97
98
//POST API (Bookings )
99
app.post("/bookings", async (req, res) => {
100
const newBooking = req.body;
101
const result = await bookingsCollection.insertOne(newBooking);
102
res.send(result);
103
});
104
105
//POST API (Testimonials )
106
app.post("/testimonials", async (req, res) => {
107
const newBooking = req.body;
108
// console.log(newBooking);
109
const result = await testimonialCollection.insertOne(newBooking);
110
res.send(result);
111
});
112
113
// ======================DELETE API ========================
114
//DELETE API(Bookings)
115
app.delete("/bookings/:id", async (req, res) => {
116
const id = req.params.id;
117
const query = { _id: ObjectId(id) };
118
const result = await bookingsCollection.deleteOne(query);
119
res.send(result);
120
});
121
122
//DELETE API(drone)
123
app.delete("/Devices/:id", async (req, res) => {
124
const id = req.params.id;
125
const query = { _id: ObjectId(id) };
126
const result = await djiPackages.deleteOne(query);
127
res.send(result);
128
});
129
130
// =================Update API====================
131
app.put("/bookings/:id", async (req, res) => {
132
const id = req.params.id;
133
const newStatus = req.body;
134
const query = { _id: ObjectId(id) };
135
const options = { upsert: true };
136
const updateDoc = {
137
$set: {
138
data: newStatus.newData,
139
},
140
};
141
const result = await bookingsCollection.updateOne(
142
query,
143
updateDoc,
144
options
145
);
146
res.send(result);
147
});
148
149
//upsert Google user data
150
app.put("/users", async (req, res) => {
151
const user = req.body;
152
const filter = { email: user.email };
153
const options = { upsert: true };
154
const updateDoc = { $set: user };
155
const result = await usersCollection.updateOne(
156
filter,
157
updateDoc,
158
options
159
);
160
res.json(result);
161
});
162
163
// add admin role
164
app.put("/users/admin", async (req, res) => {
165
const user = req.body;
166
const filter = { email: user.email };
167
const updateDoc = { $set: { role: "admin" } };
168
const result = await usersCollection.updateOne(filter, updateDoc);
169
res.json(result);
170
});
171
});
172
} finally {
173
// await client.close();
174
}
175
}