-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconnectDB.js
More file actions
49 lines (46 loc) · 1.13 KB
/
connectDB.js
File metadata and controls
49 lines (46 loc) · 1.13 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const MongoClient = require('mongodb').MongoClient;
const urlOfDB = "mongodb://127.0.0.1:27017/filemanager";
const dbName = "filemanager";
const Client = new MongoClient(urlOfDB, {
useUnifiedTopology: true
}); // 创建mongodb客户端
async function connect() {
try {
await Client.connect();
// await Client.db(dbName);
// const DB = await Client.db("filemanager");
// return await DB;
} catch (error) {
await Client.close();
console.error("数据库连接失败", error);
}
}
const DB = connect();
/**
* 连接数据库
*/
// Client.connect(error => {
// if (error) {
// throw error
// }
// console.log("连接成功");
// const DataBase = Client.db(dbName);
// Client.close();
// return DataBase;
/**
* 在user表中查数据
*/
// const userCollection = DataBase.collection('user');
// userCollection.find({}).toArray((error, result) => {
// if (error) {
// console.error("查user表错误", error);
// }
// for (const r of result) {
// console.log("查询结果:", r);
// }
// })
// })
module.exports = {
Client,
DB
};