-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
48 lines (38 loc) · 1.38 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = "mongodb://127.0.0.1:27017/loc8r";
const dbName = 'undercover';
const insertPair = function(string1, string2, name){
const client = new MongoClient(url, {useNewUrlParser: true/*, useUnifiedTopology: true*/});
client.connect(function(err) {
assert.equal(null, err);
console.log("connected to database");
const db = client.db(dbName);
const coll = db.collection('words');
coll.insertOne({normal:string1,undercover:string2,authorName:name},
function(err, result){
assert.equal(err,null);
console.log("insert sucessful");
})
client.close();
})
}
function getAll(A){
const client = new MongoClient(url, {useNewUrlParser: true, useUnifiedTopology: true});
client.connect(function (err) {
assert.equal(null, err);
console.log("connected to database");
const db = client.db(dbName);
const coll = db.collection('words');
coll.find({}).toArray().then((data) => {
//console.log(data);
//A.lenght = 0;
data.forEach((element)=>{
A.push(element);
})
console.log('fetched words');
//console.log(array);
})
})
}
module.exports = { insertPair, getAll }