0

我有这个代码片段,它在其他地方工作得很好,但是当我将它移动到不同的部分时会给我一个循环引用错误。我什至在任何地方都找不到关于什么是循环引用的好参考。

// Create a new array to hold each of the Properties from the custom search pane
// This array will eventually be converted to JSON and become a List<Property>
propertyTables = [];

// Create new Object to hold a row - we have to construct these 
// Property objects manually for each row
propertyRow = {};

// This needs to be rewritten to include all of hidden input elements from the custom object that is clicked
// For each of the data elements the properties table, add it to the Object
$(this).parent().find('.editablePropertyList .customPropertyPrompt, .editablePropertyList .customPropertyDataType, .editablePropertyList .customPropertyInquirySearchType, .editablePropertyList .customPropertyID, .editablePropertyList .customPropertyText').each(function(index) {
    propertyValue = $(this).val();
    propertyText = $(this).text();
    switch ($(this).attr("class")) {
        case "customPropertyID":
            propertyRow.propertyID = propertyValue;
            break;
        case "customPropertyDataType":
            propertyRow.dataType = propertyValue;
            break;
        case "customPropertyPrompt":
            propertyRow.prompt = propertyText;
            break;
        case "customPropertyInquirySearchType":
            propertyRow.inquirySearchType = propertyValue;
            break;
        case "customPropertyText":
            // Whenever it reaches this data element, this means
            // that the iteration is at the end of a row.  Push the
            // newly filled propertyRow object (typeof Property) on
            // the PropertyTable array.  Then reinstance the propertyRow
            // object and it will start populating with the next row
            // as the next iteration occurs with propertyID
            propertyRow.inquirySearchText = propertyValue;
            if (propertyRow.inquirySearchText !== "") {
                propertyTables.push(propertyRow);
            }
                propertyRow = {};
                break;
            }
    });


    var statusFilter = [];
    var limitAnnotation = [];


    searchCriteria = {}; // Created the object
    searchCriteria.topFolderListBox = topFoldersWithSomeSelected; // Add the List<topLevelFolders> as the first property
    searchCriteria.docTypesListBox = docTypesWithSomeSelected; // Add the List<DocumentType> as the second property
    searchCriteria.propertyTable = propertyTables; // Add the List<Property> as the third property
    searchCriteria.statusFilter = statusFilter; // Add the List<statusFilter> as the fourth property
    searchCriteria.limitAnnotation = limitAnnotation; // Add the List<limitAnnotation> as the fifth property
    searchCriteria.fromDate = ""; // Add the string fromDate as the sixth property
    searchCriteria.toDate = ""; // Add the string toDate as the seventh property
    searchCriteria.dateRangeRelativeToday = false;
    searchCriteria.annotationText = ""; // Add the string annotationText as the eigth and final property

    // Convert to JSON String - craps out here with circular reference error
    textToSend = JSON.stringify(searchCriteria, null, "");
4

1 回答 1

3

循环参考

简而言之:它是 whenobjA包含对的引用,objB而引用又包含对objA.. 的引用,依此类推。你会有一个这样的无限系列。

一个最简单的例子:

var a = {}
var b = {}
a['x'] = b;
b['y'] = a;

在上述情况下,aobject 包含一个x引用的键b。并且以类似的方式,bobject 包含一个再次y引用a back 的键。

这是序列化时的经典问题(如本例中的 JSON):

serialise a ->
    serialise value of key x in a ->  # == b
        serialise b ->
            serialise value of key y in b -> # ==a
                serialise a ->
                    ... and so on..

尝试上述代码时出现的错误(在 Chrome 中):

TypeError: Converting circular structure to JSON

关于您的问题,如果不查看整个代码,真的很难判断循环引用的确切位置。我会建议一件事。做一个console.log(searchCriteria)。如果您的浏览器显示树状结构,请继续扩展节点,直到您遇到以前见过的节点(深度较小)。

当你看到类似下面的东西时,你就知道罪魁祸首是什么了。:) 循环引用问题

于 2012-07-02T17:20:31.257 回答