1086040
var mongodb = require('./db');
var Promise = require("bluebird");
Promise.promisifyAll(mongodb);
Account.prototype.save = function save(callback){
var account = {
user: this.name,
number: this.number,
type: this.type,
time: this.time,
}
mongodb.openAsync()
.then(function(db){
Promise.promisifyAll(db);
db.collectionAsync('accounts')
.then(function(collection){
collection.ensureIndex('user');
Promise.promisifyAll(collection);
collection.insertAsync(account,{safe:true})
.then(function(account){
mongodb.close();
return callback(null,account);
})
.catch(function(err){
mongodb.close();
return callback(err,null);
});
})
.catch(function(err){
mongodb.close();
return callback(err);
});
})
.catch(function(err){
return callback(err);
});
}
这种情况下,要怎么优化一下。求大神指导!!