Right now I'm working with both mongoose 3.1.1 and async 0.1.22.
But when I tried to save Mongoose models instance inside async.auto
it just stopped working.
See the following example and try it by yourself:
mongoose = require 'mongoose'
async = require 'async'
Schema = mongoose.Schema
ObjectId = Schema.ObjectId
mongoose.connect "mongodb://localhost:27017/async-test"
SmthSchema = new Schema
data : type: String
mongoose.model 'Smth', SmthSchema
Smth = mongoose.model 'Smth'
test1 = (next) ->
console.log ' test 1'
smth = new Smth data: 'some data'
async.auto
first: (callback) ->
smth.save callback
second: ['first', (callback) ->
console.log ' it works!'
callback()]
next
test2 = (next) ->
console.log ' test 2'
smth = new Smth data: 'some data'
async.series [
smth.save.bind smth
(callback) ->
console.log ' it works!'
callback()
], next
test3 = (next) ->
console.log ' test 3'
context =
save: (callback) -> callback null
async.auto
first: context.save.bind context
second: ['first', (callback) ->
console.log ' it works!'
callback()]
next
test4 = (next) ->
console.log ' test 4'
smth = new Smth data: 'some data'
async.auto
first: smth.save.bind smth
second: ['first', (callback) ->
console.log ' it works!'
callback()]
next
console.log 'running all tests'
async.series [test1, test2, test3, test4], (err) ->
console.log err || 'all works!'
Resulting output:
running all tests
test 1
it works!
test 2
it works!
test 3
it works!
test 4
smth.save.bind smth
binds save function to the object it shall save. It works great in async.series
and async.parallel
, but not in async.auto
.
async.auto
saves the object to database, but it looses callback and processing stops.
But does anybody have any idea why it happens?
The strangest thing is than i never had any problems neither with binding anything else inside of async.auto
nor with binding Mongoose
save method in any other context.
I already looked into async
code, but I still have no idea what's wrong. Now I'm planning to write an issue about it on github.
Added 20.09.12: I replaced save
function with validate
function and all worked great:
running all tests
test 1
it works!
test 2
it works!
test 3
it works!
test 4
it works!
all works!
So the problem deeply connected to mongoose save
function.
It looks like async.auto
breaks somewhere when it works with Mongoose method save
. But I can't understand where and why.
Figured it out: If you use @dpatti's branch, it works. I guess @caolan hasn't merged it yet (it's been 6 months or so). I'm using async in our Node.js framework, Sails, and so I'll be keeping a close eye on this, so if it gets merged in, I'll post back here.
In your package.json, change your async dependency to look like this:
"async": "git://github.com/dpatti/async.git#safe-auto"
Then do an npm install
and you should be good to go.
Having the same problem when combining with Sequelize's save(). I think it's a function context issue—hope that helps as a start!
I'm working on a basic example that breaks and I'll report back when I have more information. If it looks like it's actually an issue in async, I'll raise an issue in github and link to it here.
I'm also on async 0.1.22 (and Sequelize 1.5.0-beta-2, in case that helps anybody) using raw javascript (no coffee)