mercredi 4 octobre 2017

Push data to array from 2 queries using knockoutjs and Parse

I am using KnockoutJS and Parse as a backend I am trying to push data from 2 queries but the results query gets only one result. I tried pushing the review and review_rating after the users query but it just displayed 4 rows of Reviews that has the comments and another 4 that has the users data

    var q = new Parse.Query("Comments");

q.find({
    success: function (results) {
        for (var i = 0; i < results.length; i++) {
            var q2 = new Parse.Query("User");
            q2.equalTo("objectId", results[i].get('user_id').id);
            q2.find({
                success: function (users) {
                    for (var i = 0; i < users.length; i++) {
                        self.listcomments.push({
                            review_name: users[i].get('firstname') + " " + users[i].get('lastname'),
                            review_img: users[i].get('picture')._url,
                            review: results[i].get('comment'),
                            review_rating: results[i].get('stars')+"stars"
                        });
                    }

                },
                error: function (user, error) {
                    console.log("Error: " + error.code + " " + error.message);
                    notify("Something went wrong", 'danger');
                }
            });
            [enter image description here][1]
        }
        console.log(results);
    }
    ,

    error: function (user, error) {
        console.log("Error: " + error.code + " " + error.message);
        notify("Something went wrong", 'danger');
    }
});

Here is the output , you will notice that the comment and starcount from results() is the same but the users() image and name gives the right data.

here is the output when the code is like this

 success: function (results) {
        for (var i = 0; i < results.length; i++) {
            var q2 = new Parse.Query("User");
            q2.equalTo("objectId", results[i].get('user_id').id);
            q2.find({
                success: function (users) {
                    for (var i = 0; i < users.length; i++) {
                        self.listcomments.push({
                            review_name: users[i].get('firstname') + " " + users[i].get('lastname'),
                            review_img: users[i].get('picture')._url,

                        });
                    }

                },
                error: function (user, error) {
                    console.log("Error: " + error.code + " " + error.message);
                    notify("Something went wrong", 'danger');
                }
            });
            self.listcomments.push({
                review: results[i].get('comment'),
                review_rating: results[i].get('stars')
            });
        }

enter image description here




Aucun commentaire:

Enregistrer un commentaire