现在我正在使用 mongoose 3.1.1 和 async 0.1.22。但是,当我尝试将 Mongoose 模型实例保存在async.auto
其中时,它就停止了工作。
请参阅以下示例并自行尝试:
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!'
结果输出:
running all tests
test 1
it works!
test 2
it works!
test 3
it works!
test 4
smth.save.bind smth
将保存函数绑定到它要保存的对象。async.series
它在and中效果很好async.parallel
,但在async.auto
.
async.auto
将对象保存到数据库,但它会丢失回调并且处理停止。但是有人知道为什么会这样吗?
最奇怪的是,我从来没有遇到过任何问题,无论是在内部绑定任何东西,async.auto
还是在任何其他上下文中绑定Mongoose
保存方法。
我已经查看了async
代码,但我仍然不知道出了什么问题。现在我打算在github上写一个关于它的问题。
添加了 20.09.12:我用save
函数替换了validate
函数,一切都很好:
running all tests
test 1
it works!
test 2
it works!
test 3
it works!
test 4
it works!
all works!
save
所以这个问题与猫鼬功能密切相关。
async.auto
当它与 Mongoose 方法一起使用时,它看起来像是在某处中断save
。但我不明白在哪里以及为什么。