如何解决未捕获的错误:无法序列化未保存的Parse.Object错误消息? [英] How to resolve Uncaught Error: Can't serialize an unsaved Parse.Object error message?

查看:83
本文介绍了如何解决未捕获的错误:无法序列化未保存的Parse.Object错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用parse.com和JavaScript SDK.

Using parse.com and JavaScript SDK.

我已经搜索过,但是找不到很好的答案.

I've searched, but cannot find a good answer for this.

当我仅使用当前用户时,此功能有效.但是,它应该做的是使用存储在变量friendRequest中的值来查询解析中已经存在的结果.一旦用户单击div id container

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

我收到以下错误,我不确定如何解决?是因为FriendRequest是未定义的吗?

I get the following error and I'm not sure how to fix it? Is it because FriendRequest is undefined?

未捕获的错误:无法序列化未保存的Parse.Object

Uncaught Error: Can't serialize an unsaved 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;

        });

推荐答案

如果在调用FriendProfile()时未设置window.selectedFriendRequestId,则会出现错误.在其中添加console.log(window.selectedFriendRequestId);可以帮助您进行调试.

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.

您需要检查要执行的逻辑,您可能希望将friendRequestId用作函数的参数,并将其值提取到单击处理程序中,然后将其作为参数传递.当前,您的第2节"代码正在调用该函数,然后再设置该值.

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.

简化示例:

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

这篇关于如何解决未捕获的错误:无法序列化未保存的Parse.Object错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆