node.js - Model.create retrive how many documents have been created -
i using model.create(array) in mongoose.
want provide user feedback how many documents have been created , how many of them haven't (i.e. didn't validate).
created callback
user.create(userstoimport, function(err, docs) { console.log(err); console.log(docs); }
the problem if document not validate, receive validation error on single non-valid document, while cannot retrieve information inserted documents.
there way information?
i think, need .settle()
method when.js
module.
here example of doing using when.js
mongoose 3.8.x
:
when = require('when'); promises = userstoimport.map(function(user) { return user.create(user); // returns promise }); when.settle(promises).then(function(results) { // results array, containing following elements: // { state: 'fulfilled', value: <document> } // { state: 'rejected', value: <error> } });
it's possible without promises (e.g. using async
module), code more complicated.
Comments
Post a Comment