Model.create retrive how many documents have been created - node.js

I am using Model.create(Array) in Mongoose.
I want to provide user a feedback about how many documents have been created and how many of them haven't (i.e. they didn't validate).
I created a callback like this
User.create(usersToImport, function(err, docs) {
console.log(err);
console.log(docs);
}
The problem is that if any document does not validate, I only receive a validation error on the single non-valid document, while I cannot retrieve any information about the inserted documents.
Is there any way to get this information?

I think, you need something like .settle() method from when.js module.
Here is an example of doing it using when.js with 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 is an array, containing following elements:
// { state: 'fulfilled', value: <document> }
// { state: 'rejected', value: <error> }
});
It's possible to do it without Promises (e.g. using async module), but the code will be much more complicated.

Model.create() isn't going to return that information for you. The best option would be to roll your own version of Model.create(), which is essentially a convenience method for calling Model.save() multiple times, and collate the information yourself. A good way to get started is the code for Model.create().

Related

Updating a user concurrently in mongoose using model.save() throws error

I am getting a mongoose error when I attempt to update a user field multiple times.
What I want to achieve is to update that user based on some conditions after making an API call to an external resource.
From what I observe, I am hitting both conditions at the same time in the processUser() function
hence, user.save() is getting called almost concurrently and mongoose is not happy about that throwing me this error:
MongooseError [ParallelSaveError]: Can't save() the same doc multiple times in parallel. Document: 5ea1c634c5d4455d76fa4996
I know am guilty and my code is the culprit here because I am a novice. But is there any way I can achieve my desired result without hitting this error? Thanks.
function getLikes(){
var users = [user1, user2, ...userN]
users.forEach((user) => {
processUser(user)
})
}
async function processUser(user){
var result = await makeAPICall(user.url)
// I want to update the user based on the returned value from this call
// I am updating the user using `mongoose save()`
if (result === someCondition) {
user.meta.likes += 1
user.markModified("meta.likes")
try {
await user.save()
return
} catch (error) {
console.log(error)
return
}
} else {
user.meta.likes -= 1
user.markModified("meta.likes")
try {
await user.save()
return
} catch (error) {
console.log(error)
return
}
}
}
setInterval(getLikes, 2000)
There are some issues that need to be addressed in your code.
1) processUser is an asynchronous function. Array.prototype.forEach doesn't respect asynchronous functions as documented here on MDN.
2) setInterval doesn't respect the return value of your function as documented here on MDN, therefore passing a function that returns a promise (async/await) will not behave as intended.
3) setInterval shouldn't be used with functions that could potentially take longer to run than your interval as documented here on MDN in the Usage Section near the bottom of the page.
Hitting an external api for every user every 2 seconds and reacting to the result is going to be problematic under the best of circumstances. I would start by asking myself if this is absolutely the only way to achieve my overall goal.
If it is the only way, you'll probably want to implement your solution using the recursive setTimeout() mentioned at the link in #2 above, or perhaps using an async version of setInterval() there's one on npm here

How to console.log a promisified mongoose query *without* bluebird

I am trying to run a mongoose query and write it to the console without installing yet another library like bluebird. I have been unable to find this in the documentation.
Here is the query:
function activityH(){
return setRecords.find({'item.title': 'marzipan'}
, 'item.title item.quantity').exec();
}
And the context in which I am calling it:
....[a bunch of promises].then(activityF).then(activityG).then(activityH).then(function(doc){console.log(doc);});
All of the prior activities are completing, but nothing is logging even though my query should have results. I feel this is a very basic question but I have looked for hours trying to find a solution and if this is a duplicate, the original answer is very hard to search for!
Do I absolutely need bluebird to do this? E.g. this blog post
Thank you
You could write a little logging function wrapper to help you out. Something like
function log(data) {
console.log(data);
return data;
}
And then add it your Promise chain.
....[a bunch of promises]
.then(log)
.then(activityF)
.then(log)
.then(activityG)
.then(log)
.then(activityH)
.then(log)
If you want some default messaging you could also pass a message string
function log(msg) {
return function(data) {
console.log(msg, data);
return data;
}
}
And then would add to the chain like:
activityA()
.then(log('activityA'))
.then(activityB)
.then(log('activityB'))

how to handle nested for each loop which contain callback using sails.js

I have pseudo code -
foreach(clients){
foreach(orderids)
{
//call asynchronous findOne()
if(client_Has_OrderId){
count++;
}
}//close foreach orderids
storeInArray(client,count);
}//close client foreach loop
I am new on sails.js and don't know how to code this in sails.js I have no experience of asynchronous programming.When I code this in synchronous way desire result not get.
result should look like-
client1 1
client2 5
client3 0
Thank you.
I want to recommend you to have a look at the sails reference and also the concepts section.
It always depends on your setup: I assumed you have set up a foreign key to your orders (see OneToMany), so you can just populate your values and directly access them in the promise, which would be the optimized way to query your desired result.
ES 5 compatible code could look like:
Note that I am using lodash (or underscore) for validating the array - which is included by default in sails.js if you did't disable it in the config.
Client.find({your:'criteria'})
.populate('orders')
.then(function(clients){
var orderIndex = {};
_.forEach(clients, function(client, index){
if(!_.isArray(client.orders)) {
orderIndex[client.id] = 0;
} else {
orderIndex[client.id] = client.orders.length;
});
// do something with [orderIndex]
})
.catch(function(e){
console.log(e);
})

How do you save multiple records in SailsJS in one action?

I am wondering what the best practice would be for saving multiple records in an action that makes changes, particularly add and remove, to multiple records. The end goal is to have one function have access to all of the changed data in the action. Currently I am nesting the saves in order for the innermost saves to have access to the data in all the updated records. Here is an example of how I am saving:
record1.save(function (error, firstRecord) {
record2.save(function (erro, secondRecord) {
record3.save(function (err, thirdRecord) {
res.send({recordOne: firstRecord, recordTwo: secondRecord, recordThree: thirdRecord});
});
});
});
With this structure of saving, recordOne, recordTwo, and recordThree display the expected values on the server. However, checking localhost/1337/modelName reveals that the models did not properly update and have incorrect data.
You could use the built in promise engine Bluebird to to that.
Promise.then(function() {
return [record1.save(),record2.save(),record3.save()];
}).spread(function(record1_saved,record2_saved,record3_saved){
res.send({recordOne: record1_saved, recordTwo: record2_saved, recordThree: record3_saved});
req.namespamce = this;
}).catch(function(err){
res.badRequest({
error: err.message
});
req.namespamce = this;
});

Synchroneous call with mongodbnative

I have the following problem with mongodb-native. I have a function whose purpose is to return some element from db.
function get(){
db.collection('test').findOne({...},{...},function(err, doc){
// my doc is here
});
// but here my doc is undefined
// so I can not return it
return doc;
}
So due to asynchroneous nature of node, I can not get my doc back. So how can I make it synchroneous (or is there any other way)?
I need for my single page app. So when my client does some action, ajax request is sent which is handled by get function. This function has to send back JSON.
The answer is to have your code work asynchroneous, that is the whole concept of JavaScript.
Thus if you have something like this in your synchroneous program:
function get() {
db.collection('test').findOne({...},{...},function(err,doc) {
});
return doc;
}
var doc = get();
console.log(doc);
You can refactor it into:
function printToConsole(err, doc) {
console.log(doc);
}
function get(callback) {
db.collection('test').findOne({...},{...},callback);
}
get(printToConsole);
It is not a unique solution, there are other workflows. Some like promises will make your code flatter.
But my personal suggestion, is to initially learn how to code asynchroneous code without supporting libraries. Initially it feels like a pain, just give it some time and you will start enjoying the idea.

Resources