Skip to content

Commit

Permalink
mongoose.connection.on(open|error)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardhotchkiss authored and Edward Hotchkiss committed Dec 23, 2011
1 parent 4e934ad commit 4dfe795
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/short.js
Expand Up @@ -33,6 +33,12 @@ function hasher(URL) {

exports.connect = function (mongodb) {
mongoose.connect(mongodb);
mongoose.connection.on('open', function(){
console.log('mongodb connected');
});
mongoose.connection.on('error', function(error){
throw new Error(error);
})
};

/*!
Expand All @@ -43,15 +49,14 @@ exports.connect = function (mongodb) {

exports.gen = function (URL, callback) {
var hashedURL = hasher(URL);

var item = new ShortURL({
URL : URL,
hash : hashedURL
});
item.save(function (error, item) {
//Tries to save to mongodb, if it exists it retries
if (error && error.code === 11000) {
console.log(hashedURL + " already exists! Retrying!");
console.log(hashedURL + ' already exists! Retrying!');
short.gen(URL, callback);
} else {
callback(null, item);
Expand Down

0 comments on commit 4dfe795

Please sign in to comment.