Skip to content

Commit 200e191

Browse files
author
Joe Alves
committed
Mongoose now uses bluebird promises; implemented .catch where previously unavailable; couple random things to quiet the linter
1 parent 4244847 commit 200e191

File tree

8 files changed

+39
-27
lines changed

8 files changed

+39
-27
lines changed

generated/seed.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ var mongoose = require('mongoose');
2121
var Promise = require('bluebird');
2222
var chalk = require('chalk');
2323
var connectToDb = require('./server/db');
24-
var User = Promise.promisifyAll(mongoose.model('User'));
24+
var User = mongoose.model('User');
25+
26+
var wipeCollections = function () {
27+
var removeUsers = User.remove({});
28+
return Promise.all([
29+
removeUsers
30+
]);
31+
};
2532

2633
var seedUsers = function () {
2734

@@ -36,23 +43,22 @@ var seedUsers = function () {
3643
}
3744
];
3845

39-
return User.createAsync(users);
46+
return User.create(users);
4047

4148
};
4249

43-
connectToDb.then(function () {
44-
User.findAsync({}).then(function (users) {
45-
if (users.length === 0) {
46-
return seedUsers();
47-
} else {
48-
console.log(chalk.magenta('Seems to already be user data, exiting!'));
49-
process.kill(0);
50-
}
51-
}).then(function () {
50+
connectToDb
51+
.then(function () {
52+
return wipeCollections();
53+
})
54+
.then(function () {
55+
return seedUsers();
56+
})
57+
.then(function () {
5258
console.log(chalk.green('Seed successful!'));
5359
process.kill(0);
54-
}).catch(function (err) {
60+
})
61+
.catch(function (err) {
5562
console.error(err);
5663
process.kill(1);
5764
});
58-
});

generated/server/app/configure/authentication/facebook.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ module.exports = function (app) {
2929
});
3030
}
3131

32-
}).then(function (userToLogin) {
32+
})
33+
.then(function (userToLogin) {
3334
done(null, userToLogin);
34-
}, function (err) {
35+
})
36+
.catch(function (err) {
3537
console.error('Error creating user from Facebook authentication', err);
3638
done(err);
3739
})

generated/server/app/configure/authentication/google.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ module.exports = function (app) {
3030
});
3131
}
3232

33-
}).then(function (userToLogin) {
33+
})
34+
.then(function (userToLogin) {
3435
done(null, userToLogin);
35-
}, function (err) {
36+
})
37+
.catch(function (err) {
3638
console.error('Error creating user from Google authentication', err);
3739
done(err);
3840
});

generated/server/app/configure/authentication/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
var session = require('express-session');
33
var MongoStore = require('connect-mongo')(session);
4-
var _ = require('lodash');
54
var passport = require('passport');
65
var path = require('path');
76
var mongoose = require('mongoose');
@@ -64,4 +63,4 @@ module.exports = function (app) {
6463
require(path.join(__dirname, strategyName))(app);
6564
});
6665

67-
};
66+
};

generated/server/app/configure/authentication/local.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
var passport = require('passport');
3-
var _ = require('lodash');
43
var LocalStrategy = require('passport-local').Strategy;
54
var mongoose = require('mongoose');
65
var User = mongoose.model('User');
@@ -19,9 +18,8 @@ module.exports = function (app) {
1918
// Properly authenticated.
2019
done(null, user);
2120
}
22-
}, function (err) {
23-
done(err);
24-
});
21+
})
22+
.catch(done);
2523
};
2624

2725
passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' }, strategyFn));

generated/server/app/configure/authentication/twitter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ module.exports = function (app) {
4545
} else { // If this twitter id has never been seen before and no user is attached.
4646
return createNewUser(token, tokenSecret, profile);
4747
}
48-
}).then(function (user) {
48+
})
49+
.then(function (user) {
4950
done(null, user);
50-
}, function (err) {
51+
})
52+
.catch(function (err) {
5153
console.error('Error creating user from Twitter authentication', err);
5254
done(err);
5355
});

generated/server/db/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var DATABASE_URI = require(path.join(__dirname, '../env')).DATABASE_URI;
88
var mongoose = require('mongoose');
99
var db = mongoose.connect(DATABASE_URI).connection;
1010

11+
// Promises returned from mongoose queries/operations are BLUEBIRD promises
12+
mongoose.Promise = Promise;
13+
1114
// Require our models -- these should register the model into mongoose
1215
// so the rest of the application can simply call mongoose.model('User')
1316
// anywhere the User model needs to be used.

generated/server/db/models/user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var schema = new mongoose.Schema({
2828
});
2929

3030
// method to remove sensitive information from user objects before sending them out
31-
schema.methods.sanitize = function () {
31+
schema.methods.sanitize = function () {
3232
return _.omit(this.toJSON(), ['password', 'salt']);
3333
};
3434

@@ -63,4 +63,4 @@ schema.method('correctPassword', function (candidatePassword) {
6363
return encryptPassword(candidatePassword, this.salt) === this.password;
6464
});
6565

66-
mongoose.model('User', schema);
66+
mongoose.model('User', schema);

0 commit comments

Comments
 (0)