0

Using Parse.com and JavaScript SDK.

The purpose of the below function is:

a/ Return a list of users that the current user needs to respond to a friend request. b/ When they click cancel, the current object state changes from "Pending" to "Declined"

Part (a) works as required. Part (b) is returning the following error

Uncaught TypeError: Cannot call method 'get' of undefined

I presume this is because it cannot find "status" to change and save it from "Pending" to "Declined" ?

I've blocked out the area of code that I think is not working, but cannot get around this and need some help ;-)

                     //////////Returns list of Friends pending a response///////////////////
                         function FriendsPending() {

                            $('#containerFriends').empty();
                            $('#containerFriendsRejected').empty();
                            $('#containerFriendsRequestSent').empty();
                            $('#containerFriendsConnected').empty();
                            $('#userimgs').empty();

                            var currentUser = Parse.User.current();
                            var FriendRequest = Parse.Object.extend("FriendRequest");

                            var query = new Parse.Query(FriendRequest);
                            query.include('toUser');
                            query.include('SentTo');
                            query.include("myBadge");
                            query.equalTo("fromUser", currentUser);
                            query.equalTo("status", "Pending");

                            query.find({
                                success: function(results) {
                                    var friends = [];
                                    for (var i = 0; i < results.length; i++) {
                                        friends.push({
                                            imageURL: results[i].get('toUser').get('pic'),
                                            friendRequestId: results[i].id,
                                            username: results[i].get('toUser').get('username'),
                                            userId: results[i].get('toUser').id,
                                            status: results[i].get('status').id


                                        });


                                    }
                                    var select = document.getElementById("FriendsPending");
                                    $.each(friends, function(i, v) {
                                        var opt = v.username;
                                        var el = document.createElement("option");
                                        el.textContent = opt;
                                        el.value = opt;
                                        select.appendChild(el);
                                    })

                                    $('#containerFriendsPending').empty();
                                    _.each(friends, function(item) {
                                        var wrapper = $('<div class="portfolio-item-thumb one-third"></div>');
                                        wrapper.append('<img class="responsive-image friendImgOutline" src="' + item.imageURL + '" />'+ '<br>');
                                        wrapper.append('<div class="tag">' + item.username + '</div>');
                                        $('#containerFriends').append(wrapper);
                                        wrapper.append('<div type="button" class="btn btn-success mrs">' + 'Accept' + '</div>');
                                        wrapper.append('<div type="button" class="btn btn-danger mrs decline">' + 'Decline' + '</div>');
                                        $('#containerFriendsPending').append(wrapper);
//////Code with issues//////////////
                                        $(document).on('click', function() {
                                            $(".decline").click(function() {
                                                status: results[i].get('status').id
                                                status.set("status", "Declined");
                                                console.log(status);
///////////////////////////
                                                status.save(null, {
                                                    success: function(results) {
                                                            // The object was saved successfully.
                                                            location.reload();
                                                        },
                                                        error: function(contact, error) {
                                                            // The save failed.
                                                            // error is a Parse.Error with an error code and description.
                                                            alert("Error: " + error.code + " " + error.message);
                                                        }
                                                    });


                                            });

                                        });
                                    });

},
error: function(error) {
    alert("Error: " + error.code + " " + error.message);
}
});

}

1 Answer 1

2
status: results[i].get('status').id

You are getting that error because you are trying to call get on results[i] which is undefined because i is undefined.

Also, the line itself is not a valid expression. If you want to assign something to a variable called "status", this is the correct syntax:

var status = ...

Further, it looks like you are treating status as an object, rather than just a field.

status: results[i].get('status')

When you want to change the status later, you need to keep a reference to the original fetched object. E.g.:

friends.push({
    imageURL: results[i].get('toUser').get('pic'),
    friendRequestId: results[i].id,
    username: results[i].get('toUser').get('username'),
    userId: results[i].get('toUser').id,
    status: results[i].get('status'),
    fetchedObject: results[i]
});

Then later when you want to set the status on that object to declined:

item.fetchedObject.set("status", "Declined");

It is the fetchedObject you then want to save:

item.fetchedObject.save( // );
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, that looks like it moves me in the right direction, its returning a "Uncaught TypeError: Object Pending has no method 'set' " error now though, any ideas?
Looks like you don't have the full status object. Fetch it or include it in the original query.
Sorry, from looking at your code I thought status was a class on its own. I updating the answer now.
thank you so much, this is perfect - Thank you for explaining it so clearly.

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.