Skip to content

Commit f9af9e2

Browse files
authored
Instead of promisifying all methods of mongoose, plugin bluebird into… (#39) (#40)
* Instead of promisifying all methods of mongoose, plugin bluebird into mongoose * Update node version in travis config
1 parent d2510e2 commit f9af9e2

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: node_js
22
node_js:
3-
- "4.4"
4-
- "6.3"
3+
- "4.5"
4+
- "6.6"
55
services:
66
- mongodb
77
cache:

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import mongoose from 'mongoose';
33
import config from './config/env';
44
import app from './config/express';
55

6-
// promisify mongoose
7-
Promise.promisifyAll(mongoose);
6+
// plugin bluebird promise in mongoose
7+
mongoose.Promise = Promise;
88

99
// connect to mongo db
1010
mongoose.connect(config.db, { server: { socketOptions: { keepAlive: 1 } } });

server/controllers/user.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import User from '../models/user';
44
* Load user and append to req.
55
*/
66
function load(req, res, next, id) {
7-
User.get(id).then((user) => {
8-
req.user = user; // eslint-disable-line no-param-reassign
9-
return next();
10-
}).error((e) => next(e));
7+
User.get(id)
8+
.then((user) => {
9+
req.user = user; // eslint-disable-line no-param-reassign
10+
return next();
11+
})
12+
.catch((e) => next(e));
1113
}
1214

1315
/**
@@ -30,9 +32,9 @@ function create(req, res, next) {
3032
mobileNumber: req.body.mobileNumber
3133
});
3234

33-
user.saveAsync()
35+
user.save()
3436
.then((savedUser) => res.json(savedUser))
35-
.error((e) => next(e));
37+
.catch((e) => next(e));
3638
}
3739

3840
/**
@@ -46,9 +48,9 @@ function update(req, res, next) {
4648
user.username = req.body.username;
4749
user.mobileNumber = req.body.mobileNumber;
4850

49-
user.saveAsync()
51+
user.save()
5052
.then((savedUser) => res.json(savedUser))
51-
.error((e) => next(e));
53+
.catch((e) => next(e));
5254
}
5355

5456
/**
@@ -59,8 +61,9 @@ function update(req, res, next) {
5961
*/
6062
function list(req, res, next) {
6163
const { limit = 50, skip = 0 } = req.query;
62-
User.list({ limit, skip }).then((users) => res.json(users))
63-
.error((e) => next(e));
64+
User.list({ limit, skip })
65+
.then((users) => res.json(users))
66+
.catch((e) => next(e));
6467
}
6568

6669
/**
@@ -69,9 +72,9 @@ function list(req, res, next) {
6972
*/
7073
function remove(req, res, next) {
7174
const user = req.user;
72-
user.removeAsync()
75+
user.remove()
7376
.then((deletedUser) => res.json(deletedUser))
74-
.error((e) => next(e));
77+
.catch((e) => next(e));
7578
}
7679

7780
export default { load, get, create, update, list, remove };

server/models/user.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ UserSchema.statics = {
4646
*/
4747
get(id) {
4848
return this.findById(id)
49-
.execAsync().then((user) => {
49+
.exec()
50+
.then((user) => {
5051
if (user) {
5152
return user;
5253
}
@@ -66,7 +67,7 @@ UserSchema.statics = {
6667
.sort({ createdAt: -1 })
6768
.skip(skip)
6869
.limit(limit)
69-
.execAsync();
70+
.exec();
7071
}
7172
};
7273

0 commit comments

Comments
 (0)