"Update Integer Value"功能不完全准确



我正在使用javascript函数来跟踪/更新文件夹中的文件数量。它在大多数情况下都非常准确,但误差幅度约为 10%。我认为这与"totalItemCount"变量未正确更新有关。我已经找了几个小时,但找不到我可能搞砸的地方。这是一个很长的文件,但实际上您所要查看的只是"totalItemCount"变量

new ApiClient({
    //marketing (69 Files, 40 Folders, 109 total, 204MB): 7f6d706e-6754-e411-b5b8-d4ae5294c399
    //tgsr/...governence (1,302 Files, 130 Folders, 1432 total): bba3bd73-f855-e411-b5b8-d4ae5294c399
    //infrastructureSoftware(): 7009c1d2-7e67-e511-80cc-000af7703bc2
    //  HELM Platform: bbab7b02-4e39-4287-abd2-445688cf4fb1
    //bbl... 6ce640d2-722f-48b4-a1de-7a059305b6c3
    apimethod: 'objects/95750c6b-84f5-4587-8b86-b559551f7660/children/view',
    method: 'get',
    queryparams: {
        maxcount: 8000,
        startindex: 0,
        includefuturepublished: true
    },
    onSuccess: function (responseText) 
    {
        var result = JSON.parse(responseText);
        var originalObject = result; //first, top-level object
        var totalItemCount = 0;
        var filesize = 0;
        totalItemCount += parseInt(result.response.totalCount);
        //Check if object has children and add to totalItemCount accordingly FOR EACH object:
        function getItemsRecursively(totalItemCount1, filesize1)
        {
            for(var i = 0; i < parseInt(totalItemCount); i++) //at this point, totalCount == #objects at this lvl
            {
                var currentObject = result.response.items[i];
                if(currentObject.size != undefined)
                {
                    filesize += currentObject.size;
                    filesize1 = filesize;
                }

                if(currentObject.numchildren > 0 && currentObject.numchildren != undefined)
                {
                    getChildrenItemCount(totalItemCount1, currentObject, filesize1);
                }
            }
        }
        function getChildrenItemCount(totalItemCount2, previousObject, filesize2)
        {
            //totalItemCount2 = totalItemCount;
            var childID = previousObject.id;
            new ApiClient
            ({
                apimethod: 'objects/' + childID + '/children/view',
                method: 'get',
                queryparams: {
                    maxcount: 8000,
                    startindex: 0,
                    includefuturepublished: true
                },
                onSuccess: function (responseText) 
                {
                    var result = JSON.parse(responseText);
                    var currentObject = result.response;
                    var currentFolderItemCount = currentObject.totalCount;
                    for(var i = 0; i < parseInt(currentFolderItemCount); i++) //at this point, totalCount == #objects at this lvl
                    {
                        var currentObject = result.response.items[i];
                        if(currentObject.size != undefined)
                        {
                            filesize += currentObject.size;
                            filesize2 = filesize;
                        }
                        if(currentObject.numchildren > 0 && currentObject.numchildren != undefined)
                        {
                            totalItemCount += parseInt(currentObject.numchildren);
                            totalItemCount2 = totalItemCount;
                            getChildrenItemCount(totalItemCount2, currentObject, filesize2);
                        }
                        //var filesize = currentObject.size;
                    }
                }
            })
        }
        getItemsRecursively(totalItemCount, filesize);
    }
})

我无法与我当前的代表发表评论,因此很抱歉 SO 社区发布作为答案。

很高兴看到totalCount是如何设置的(它说result.response.totalCount(。

另外,我建议取出所有console.log。这使得阅读代码变得非常困难。

如果文件夹包含文件夹,它是否适合您?如果是这样,您的代码中是否有该逻辑?

一旦您更新了我的建议,我很乐意再看一眼。

更新:尝试此代码。我无法测试,所以你会让我知道它是如何工作的。

var totalItemCount;
new ApiClient({
apimethod: 'objects/95750c6b-84f5-4587-8b86-b559551f7660/children/view',
method: 'get',
queryparams: {
    maxcount: 8000,
    startindex: 0,
    includefuturepublished: true
},
onSuccess: function (responseText) {
    var result = JSON.parse(responseText);
    totalItemCount = parseInt(result.response.totalCount);
    getItemsRecursively();
}
});
function getItemsRecursively() {
    for(var i = 0; i < parseInt(totalItemCount); i++) {
        var currentObject = result.response.items[i];
        if(currentObject.size != undefined) {
            filesize += currentObject.size;
            if(currentObject.numchildren > 0 && currentObject.numchildren != undefined) {
                getChildrenItemCount(currentObject);
            }
        }
    }
}

function getChildrenItemCount(previousObject) {
    var childID = previousObject.id;
    new ApiClient
    ({
        apimethod: 'objects/' + childID + '/children/view',
        method: 'get',
        queryparams: {
            maxcount: 8000,
            startindex: 0,
            includefuturepublished: true
        },
        onSuccess: function (responseText) 
        {
            var result = JSON.parse(responseText);
            var currentFolderItemCount = result.response.totalCount;
            for(var i = 0; i < parseInt(currentFolderItemCount); i++) 
            {
                var currentObject = result.response.items[i];
                if(currentObject.numchildren > 0 && currentObject.numchildren != undefined)
                {
                    totalItemCount += parseInt(currentObject.numchildren);
                    getChildrenItemCount(currentObject);
                }
            }
        }
    })
}

最新更新