-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.js
33 lines (26 loc) · 829 Bytes
/
insert.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
/*
Connects to mongo and insert data
*/
var MongoClient = require('mongodb').MongoClient;
var waterfall = require('async').waterfall;
module.exports = function(ctx, cb) {
var MONGO_URL = ctx.data.MONGO_URL;
if (!MONGO_URL) return cb(new Error('MONGO_URL secret is missing'))
waterfall([
function connect_to_db(done) {
MongoClient.connect(MONGO_URL, function(err, db) {
if(err) return done(err);
done(null, db);
});
},
function do_insert(db, done) {
delete ctx.data.MONGO_URL;
db
.collection('users')
.insertOne(ctx.data, function (err, result) {
if(err) return done(err);
done(null, 'succesful');
});
}
], cb);
};