-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (31 loc) · 883 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
35
36
37
38
const fs = require('fs')
const path = require('path')
var mongodb = require('mongodb')
var MongoClient = mongodb.MongoClient
const DB_CONFIG_PATH = path.join(__dirname, '/db.config')
if(fs.existsSync(DB_CONFIG_PATH)) {
process.env.MONGO = fs.readFileSync(path.join(__dirname, '/db.config'), 'utf8')
}
const connectionUrl = process.env.MONGO
//const connectionUrl = fs.readFileSync(path.join(__dirname, '../db.config'), 'utf8')
const LISTINGS_COLLECTION = 'listings'
function connect(success, failure = function(){}) {
MongoClient.connect(connectionUrl, function(err, db) {
if(err) {
failure(err)
} else {
success(db)
db.close()
}
})
}
function readListings(callback) {
connect((db) => {
db.collection(LISTINGS_COLLECTION).find({}).toArray((err, docs) => {
callback(docs)
})
})
}
readListings(function(docs){
console.log(docs);
});