I am writing a javascript code using Parse. I have User class, Subscription class and Favorite class. All users' subscription information is stored on Subscription class and their favorite item information is stored on Favorite class so it is like one to many relationship respectively.
I would like to retrieve user information, each user's subscription count and it's favorite item count and store them in an object array.
What I have tried is as follows:
query.find().then(function(objects) {
return objects;
}).then(function (objects) {
objects.forEach(function(object) {
var subscription = Parse.Object.extend('Subscription');
var queryForSubscription = new Parse.Query(subscription);
queryForSubscription.equalTo('subscriptionUser', object);
queryForSubscription.find().then(function(subscriptions) {
var userEmail = object.get('email');
var subscriptionCount = subscriptions.length;
var sub = [
userEmail,
subscriptionCount
];
userArray.push(sub);
}
});
This code works well to get Subscription count but I am not too sure how I can also get Favorite count in parallel and save it into the array at the same time.
For example,there are User A and User B and the expected result will look like this in the end:
[{User A's email, A's subscription count, A's favorite count},
{User B's email, B's subscription count, B's favorite count}]
I am not too sure where to put a new query for Favourite class...Need your advice. Thank you in advance.