Skip to content

Instantly share code, notes, and snippets.

@yamadapc
Created March 26, 2014 17:37
Show Gist options
  • Save yamadapc/9788938 to your computer and use it in GitHub Desktop.
Save yamadapc/9788938 to your computer and use it in GitHub Desktop.
'use strict';
var mongoose = require('mongoose'),
Promise = require('bluebird');
mongoose.connect('mongodb://localhost/test');
var SomethingSchema = new mongoose.Schema({
name: String
});
SomethingSchema.pre('save', function(next) {
this.name = 'something';
next();
});
var Something = mongoose.model('Something', SomethingSchema);
Promise.promisifyAll(mongoose.Model);
Promise.promisifyAll(mongoose.Model.prototype);
/**
* Using saveAsync
* --------------------------------------------------------------------------*/
var somethingAsync = new Something({ name: 'asdf' });
somethingAsync.saveAsync()
.spread(function(something) {
console.log('Using `saveAsync` =>', something.name); // asdf
});
/**
* Using save
* --------------------------------------------------------------------------*/
var something = new Something({ name: 'asdf' });
something.save(function(err, something) {
console.log('Using `save` =>', something.name); // something
});
/**
* Using Something.prototype.save.call
* --------------------------------------------------------------------------*/
var somethingWeird = new Something({ name: 'asdf' });
Something.prototype.save.call(somethingWeird, function(err, something) {
console.log('Using `Something.prototype.save.call` =>', something.name); // => asdf
});
/**
* Using something.save.call
* --------------------------------------------------------------------------*/
var somethingWeirder = new Something({ name: 'asdf '});
something.save.call(somethingWeirder, function(err, something) {
console.log('something.save.call =>', something.name); // => something
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment