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

循環参照

要するに:それはobjA、参照がobjB含まれ、次にobjA..などへの参照が含まれる場合です。あなたはそのような無限のシリーズを持つでしょう。

最も簡単な例:

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

上記の場合、オブジェクトには。を参照aするキーが含まれています。同様に、オブジェクトには、 再び参照するキーが含まれています。xbbya

これは、シリアル化中の典型的な問題です(この場合の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 に答える