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);
}
});
}