I'm starting with node mongoose etc... I'm a good student and so I follow the doc : http://mongoosejs.com/docs/populate.html .
After a while using this syntax I ended up with the infamous callback hell. After reading a lot about how to avoid this situation I decided to go for the promise way ^^. Also async module is really tempting but I don't know if it's suited for what I want to achieve.
I just want to save an object with all the corresponding refs. According to the doc we have :
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
And for saving ref I use :
var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 });
aaron.save(function (err) {
if (err) return handleError(err);
var story1 = new Story({
title: "Once upon a timex.",
_creator: aaron._id // assign the _id from the person
});
story1.save(function (err) {
if (err) return handleError(err);
aaron.stories.push(story1); // I added those two lines
aaron.save(callback); // in order to save the refs to children
// thats it!
});
})
How can I achieve the same thing using promises the proper way ? or maybe Async is more suited ? I'm lost help ?
TBH is super verbose not sure if there is a shorter way to deal with this
Try this? (may not be syntactically exact.. typed on fly)
var save = function(aaron){
return new RSVP.Promise(function(resolve, reject){
aaron.save(function(err){
if(err){
reject(err);
}else{
resolve(aaron);
}
});
});
};
var addStory = function(aaron{
return new RSVP.Promise(function(resolve, reject){
var story1 = new Story({
title: "Once upon a timex.",
_creator: aaron._id // assign the _id from the person
});
story1.save(function(err){
if(err){
reject(err);
}else{
resolve(story1, arron);
}
});
});
};
//then to exec the promises
save(arron).then(function(arron){
addStory(arron);
}).then(function(story1, arron)){
// tell the story?
}).catch(function(err){
handleError(err);
});
EDIT
This should make it short
var saves = function(objToSave){
return new RSVP.Promise(function(resolve, reject){
objToSave.save(function(err){
if(err){
reject(err);
}else{
resolve(objToSave);
}
})
});
}
var _scope = this;
saves(arron).then(function(arron){
var story1 = new Story({
title: "Once upon a timex.",
_creator: aaron._id // assign the _id from the person
});
_scope.saves(story1)
}).then(function(story1){
//do something with the story
}).catch(function(err){
handleError(err);
});
Here is another example of how you could do it with promises:
function save(obj) {
return new Promise(function (resolve, reject) {
obj.save(function (err) {
if (err) {
reject(err);
} else {
resolve(obj);
}
});
});
}
var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 });
save(aaron).then(function (savedAaron) {
var story1 = new Story({
title: 'Once upon a timex.',
_creator: savedAaron._id
});
save(story1).then(function (savedStory) {
aaron.stories.push(savedStory);
aaron.save(callback);
}, function (err) {
handleError(err);
});
}, function (err) {
handleError(err);
});
I recommend reading this article on promises: http://www.html5rocks.com/en/tutorials/es6/promises/