Note that there are some explanatory texts on larger screens.

plurals
  1. POCallback to find() is called multiple times
    primarykey
    data
    text
    <p>I'm messing around with Mongoose, and I'm noticing that my <code>find</code> callback is being called multiple times when saving an invalid item:</p> <p><strong>persist.js</strong></p> <pre><code>mongoose.model('ContentTag', new mongoose.Schema({ content: requiredString, tag: requiredString })); </code></pre> <p><strong>test.js</strong> (using mocha)</p> <pre><code>describe('content tag', function() { it('should not save an empty content tag', function(done) { persist.ContentTag().save(function(err) { assert.ok(err); persist.ContentTag.find(function(err, items) { assert.equal(0, items.length); done(); }); }); }); </code></pre> <p>This produces the following error output:</p> <pre><code>Save err: ValidatorError: Validator "required" failed for path tag Save err: ValidatorError: Validator "required" failed for path content Find err: null Find err: null </code></pre> <p>And the following error:</p> <pre><code> 1) Persist content tag should not save an empty content tag: Error: done() called multiple times at multiple (/usr/local/lib/node_modules/mocha/lib/runnable.js:168:31) at done (/usr/local/lib/node_modules/mocha/lib/runnable.js:174:26) at Runnable.run.duration (/usr/local/lib/node_modules/mocha/lib/runnable.js:190:9) at persist.ContentTag.content (path/test/server-test.js:21:21) at Query.execFind (path/node_modules/mongoose/lib/mongoose/query.js:572:26) at Cursor.toArray (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:122:15) at Cursor.each (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:167:11) at Cursor.nextObject (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:467:28) at Cursor.close (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:625:17) at Cursor.nextObject (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:467:10) at Cursor.nextObject.commandHandler (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/cursor.js:452:15) at EventEmitter.emit (events.js:96:17) at Db.open.self.serverConfig.connection.addListener.self.state (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/db.js:90:12) at EventEmitter.emit (events.js:93:17) at Connection.open.receiveListener (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/connection.js:82:16) at Socket.Connection.open.receiveListener (path/node_modules/mongoose/support/node-mongodb-native/lib/mongodb/connection.js:85:16) at Socket.EventEmitter.emit (events.js:93:17) at TCP.onread (net.js:391:31) </code></pre> <p><strong>It looks like when there are multiple validation errors, the save callback is called once for each error.</strong></p> <p>When I remove the <code>new</code> keyword, I get the following error:</p> <pre><code> 1) Persist content tag should not save an empty content tag: TypeError: Object #&lt;Object&gt; has no method 'buildDoc' at Object.Document (../node_modules/mongoose/lib/mongoose/document.js:26:19) at Object.Model (../node_modules/mongoose/lib/mongoose/model.js:24:12) at Object.model [as ContentTag] </code></pre>
    singulars
    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.
    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