【问题标题】:Binding Mongoose models save method inside async.auto (NodeJs)在 async.auto (NodeJs) 中绑定 Mongoose 模型保存方法
【发布时间】:2012-09-07 20:24:07
【问题描述】:

现在我正在使用 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.seriesasync.parallel 中效果很好,但在async.auto 中效果不佳。

async.auto 将对象保存到数据库,但它失去了回调并且处理停止。 但是有人知道为什么会发生吗?

最奇怪的是,我在绑定async.auto 内的任何其他内容以及在任何其他上下文中绑定Mongoose 保存方法时都没有遇到任何问题。

我已经查看了async 代码,但我仍然不知道出了什么问题。现在我打算在github上写一个关于它的问题。

添加 20.09.12: 我用 validate 函数替换了 save 函数,一切都很好:

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 一起使用时会在某处中断。但我不明白在哪里以及为什么。

【问题讨论】:

  • 代码对我来说看起来不错。您是否检查了生成的 JavaScript 是否可能是 CoffeeScript 问题?

标签: node.js asynchronous coffeescript mongoose


【解决方案1】:

编辑

想通了:如果你使用@dpatti 的分支,它就可以工作。我猜@caolan 还没有合并它(已经 6 个月左右)。我在我们的 Node.js 框架中使用异步,Sails,所以我会密切关注这个,所以如果它被合并,我会在这里发回。

解决方案

在您的 package.json 中,将您的异步依赖项更改为如下所示:

"async": "git://github.com/dpatti/async.git#safe-auto"

然后做一个npm install,你应该很高兴。


与 Sequelize 的 save() 组合时遇到同样的问题。我认为这是一个函数上下文问题——希望对您有所帮助!

我正在研究一个中断的基本示例,当我有更多信息时我会报告。如果看起来它实际上是异步的问题,我会在 github 中提出问题并在此处链接。

我还在使用原始 javascript(不喝咖啡)进行异步 0.1.22(和 Sequelize 1.5.0-beta-2,以防万一)

【讨论】:

  • 我已经raised an issue,但是还没有效果。
  • 它有助于多次调用,但不能绑定 monngoose save 方法。我的第一个例子仍然是中断。
  • 曹兰只是closed my issue,但我的问题仍然存在。但是async 2.0.x 会解决你的问题。它现在看起来更像是节点问题,因为function.bind 改变了async.auto 的行为。
  • 听起来他在使用_.defer()setTiemout(0) 并且它打乱了你的调用链的顺序——是这样吗?如果是这样,我最快的解决方法是将调用 save() 包装在一个异步函数中并在底部执行 _.defer(cb) (其中 cb 是您来自异步的回调函数)
  • 调用的顺序没有弄乱,它被破坏了。 smth.save.bind smth 较新的调用中的回调,因此异步流只是卡在那里。我通过将bind 替换为等效的(cb) -> smth.save cb 来解决它。仅当我组合async.automongoose.Model.savebind 时才会出现此问题。我会尝试使用_.deferprocess.nextTick,但可能不会有帮助。
猜你喜欢
  • 2015-09-29
  • 1970-01-01
  • 2020-10-22
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 2014-07-12
相关资源
最近更新 更多