2

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.

1 Answer 1

1

What if you kept columns, say in the User class, that kept count of subscriptions and favorites? So, when user clicks "favorite", a request is made to increment the respective column (there is a handy parseObject.increment("key") method available). This would save you a call or two in queries per user. If you returned 4 users, that's the difference between 4 queries and 12.

Or, instead of using queryForSubscription.find(), you could use queryForSubscription.count(). This would save a lot of information being requested so you would get the number right away instead of having to do queryResult.length;

I apologize for not answering the question you wanted. I suck with promises syntax, but here's an effort nesting the next query:

  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.count().then(function(subscriptionCount) {

            var favorite = Parse.Object.extend('Favorite');
            var queryForFavorite = new Parse.Query(favorite);
            queryForFavorite.equalTo('favoriteUser', object);           
            queryForFavorite.count().then(function(favoritesCount) {

                var userEmail = object.get('email');
                //var subscriptionCount = subscriptions.length; //used count instead

                var sub = [
                userEmail,
                subscriptionCount,
                favoritesCount
                ];
                userArray.push(sub);
        }
    }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer! the reason that I used "find()" instead of "count()" was because i need some other column from Subscription class. Sorry I should have mentioned this earlier! I am not too sure if it is a good approach to have extra two columns that keep a count for each class...i am a very SQL-oriented person so prolly too dumb to understand Parse Objects / Queries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.