-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
50 lines (49 loc) · 1.35 KB
/
handler.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
49
50
'use strict';
const mongoose = require('mongoose');
const mongoURL = '<insert mongo connection string here>';
const options = {
promiseLibrary: global.Promise,
useMongoClient: true
};
const User = require('./models/user.js');
module.exports.getUser = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;//defaults to true and will result in timeout
let response = {
isBase64Encoded: false,
statusCode: 200,
headers: null,
body: null
}
const email = event.pathParameters.email;
const promise = mongoose.connect(mongoURL, options);
promise.then(() => {
User.findOne({ email: email }).then((results) => {
response.body = JSON.stringify({ user: results})
callback(null, response);
});
}).catch((err) => {
callback(err);
});
}
module.exports.saveUser = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
let response = {
isBase64Encoded: false,
statusCode: 200,
headers: null,
body: null
}
let data = JSON.parse(event.body) || {};
const promise = mongoose.connect(mongoURL, options);
promise.then(() => {
let user = new User({
email: data.email
});
return user.save();
}).then(() => {
response.body = JSON.stringify({ message: 'saved'})
callback(null, response);
}).catch(error => {
callback(error);
})
}