Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding Mongoose models save method inside async.auto (NodeJs)
    primarykey
    data
    text
    <p>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 <code>async.auto</code> it just stopped working.</p> <p>See the following example and try it by yourself:</p> <pre><code>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) -&gt; console.log ' test 1' smth = new Smth data: 'some data' async.auto first: (callback) -&gt; smth.save callback second: ['first', (callback) -&gt; console.log ' it works!' callback()] next test2 = (next) -&gt; console.log ' test 2' smth = new Smth data: 'some data' async.series [ smth.save.bind smth (callback) -&gt; console.log ' it works!' callback() ], next test3 = (next) -&gt; console.log ' test 3' context = save: (callback) -&gt; callback null async.auto first: context.save.bind context second: ['first', (callback) -&gt; console.log ' it works!' callback()] next test4 = (next) -&gt; console.log ' test 4' smth = new Smth data: 'some data' async.auto first: smth.save.bind smth second: ['first', (callback) -&gt; console.log ' it works!' callback()] next console.log 'running all tests' async.series [test1, test2, test3, test4], (err) -&gt; console.log err || 'all works!' </code></pre> <p>Resulting output:</p> <pre><code>running all tests test 1 it works! test 2 it works! test 3 it works! test 4 </code></pre> <p><code>smth.save.bind smth</code> binds save function to the object it shall save. It works great in <code>async.series</code> and <code>async.parallel</code>, but not in <code>async.auto</code>.</p> <p><code>async.auto</code> saves the object to database, but it looses callback and processing stops. But does anybody have any idea <strong>why it happens?</strong></p> <p>The strangest thing is than i never had any problems neither with binding anything else inside of <code>async.auto</code> nor with binding <code>Mongoose</code> save method in any other context.</p> <p>I already looked into <code>async</code> code, but I still have no idea what's wrong. Now I'm planning to write an issue about it on github.</p> <p><strong>Added 20.09.12:</strong> I replaced <code>save</code> function with <code>validate</code> function and all worked great:</p> <pre><code>running all tests test 1 it works! test 2 it works! test 3 it works! test 4 it works! all works! </code></pre> <p>So the problem deeply connected to mongoose <code>save</code> function.</p> <p>It looks like <code>async.auto</code> breaks somewhere when it works with Mongoose method <code>save</code>. But I can't understand where and why.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload