问题
Using parse.com and JavaScript SDK.
I've searched, but cannot find a good answer for this.
This function works when I use just use current user. However what it should do is use the value stored in the variable friendRequest
to query results that already exist in parse. The FriendRequest
variable is populated once the user clicks on one of the images being displayed on the page within the div id container
I get the following error and I'm not sure how to fix it? Is it because FriendRequest
is undefined?
Uncaught Error: Can't serialize an unsaved Parse.Object
Screen shot attached.
Here is the function returning the error.
function FriendProfile() {
var FriendRequest = Parse.Object.extend("FriendRequest");
var friendRequest = new FriendRequest();
friendRequest.id = window.selectedFriendRequestId;
var myBadges = Parse.Object.extend("myBadges");
var query = new Parse.Query(myBadges);
query.equalTo("SentTo", friendRequest);
query.find({
success: function (results) {
// If the query is successful, store each image URL in an array of image URL's
imageURLs = [];
for (var i = 0; i < results.length; i++) {
var object = results[i];
imageURLs.push(object.get('BadgeName'));
}
// If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array
for (var j = 0; j < imageURLs.length; j++) {
$('#imgs').append("<img src='" + imageURLs[j] + "'/>");
}
},
error: function (error) {
// If the query is unsuccessful, report any errors
alert("Error: " + error.code + " " + error.message);
}
});
}
Here is how the friendRequest variable is populated .
var currentUser = Parse.User.current();
var FriendRequest = Parse.Object.extend("FriendRequest");
var query = new Parse.Query(FriendRequest);
query.include('toUser');
query.include("myBadge");
query.equalTo("fromUser", currentUser);
query.equalTo("status", "Request sent");
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')
});
}
// TW: replaced dynamic HTML generation with wrapper DIV that contains IMG and name DIV
_.each(friends, function (item) {
// using a wrapper so the user can click the pic or the name
var wrapper = $('<div class="wrapper" data-friend-request-id="' + item.friendRequestId + '"></div>');
wrapper.append('<img class="images" src="' + item.imageURL + '" />');
wrapper.append('<div>' + item.username + '</div>');
$('#container').append(wrapper);
});
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
///SECTION 2 -Click/Select friend image to be captured and used with section 3//////////
// set up event handler
$('#container').on('click', '.wrapper', function () {
console.log(this);
FriendProfile();
var wrapper = $(this);
console.log(wrapper.data('friendRequestId'));
// remove selected from all wrappers in case one was already selected
$('#container .wrapper').removeClass('selected');
// mark clicked wrapper as selected
wrapper.addClass('selected');
// save friendRequestId as a global that can be read by other code
window.selectedFriendRequestId = wrapper.data('friendRequestId');
// enabled button
$('#simulateAddBadge').removeAttr('disabled');
});
$(document).ready(function () {
$('.go').css('cursor', 'pointer');
$('.go').click(function (e) { // Button which will activate our modal
$(this).width(100).height(100).appendTo('#badgeselect');
});
return false;
});
回答1:
If window.selectedFriendRequestId
is not set when FriendProfile()
is called, then you will get an error. Add console.log(window.selectedFriendRequestId);
in there to help you debug.
You need to examine the logic you are after, you might instead want to make the friendRequestId
a parameter of the function and extract the value in the click handler and pass it as a parameter. Currently your "SECTION 2" code is calling the function then setting the value afterwards.
Simplified example:
function FriendProfile(friendRequestId) {
// query based on friendRequestId ...
}
// other code that calls function
$('#container').on('click', '.wrapper', function () {
var wrapper = $(this);
var friendRequestId = wrapper.data('friendRequestId');
FriendProfile(friendRequestId);
// other code ...
});
来源:https://stackoverflow.com/questions/24594975/how-to-resolve-uncaught-error-cant-serialize-an-unsaved-parse-object-error-mes