使用parse.com和JavaScript SDK。
我已经搜索过,但找不到合适的答案。
当我使用当前用户时,此功能有效。但它应该做的是使用存储在变量friendRequest
中的值来查询已经存在于解析中的结果。一旦用户点击FriendRequest
div id container
变量
我收到以下错误,我不确定如何修复它?是因为FriendRequest
未定义吗?
未捕获错误:无法序列化未保存的Parse.Object
附加屏幕截图。
以下是返回错误的函数。
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);
}
});
}
以下是如何填充friendRequest变量。
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;
});
答案 0 :(得分:1)
如果在调用window.selectedFriendRequestId
时未设置FriendProfile()
,则会出现错误。在那里添加console.log(window.selectedFriendRequestId);
以帮助您进行调试。
您需要检查您所遵循的逻辑,您可能希望将friendRequestId
作为函数的参数,并在单击处理程序中提取值并将其作为参数传递。目前您的&#34;第2节&#34;代码调用函数然后设置值。
简化示例:
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 ...
});