简体   繁体   English

数组在JavaScript中返回空

[英]Array returning empty in Javascript

This is all still pretty new to me but I am running into an interesting behavior issue when generating an array of numbers in a NodeJS application that handles .nessus result files. 这对我来说仍然很新,但是在处理.nessus结果文件的NodeJS应用程序中生成数字数组时,我遇到了一个有趣的行为问题。 First, some details on what I am trying to accomplish. 首先,关于我要完成的工作的一些细节。 My application generates an array [1234,1223,1222] of entries from an uploaded "results" file that is then used to query a mongodb instance to determine if those entries are currently in the DB. 我的应用程序从上传的“结果”文件生成条目数组[1234,1223,1222],然后将其用于查询mongodb实例以确定这些条目当前是否在数据库中。 If those entries are not currently in the mongodb instance, it redirects to a page where a user can edit them before being added. 如果这些条目当前不在mongodb实例中,它将重定向到页面,用户可以在其中添加之前对其进行编辑。 If there are no new entries, it goes to a page to generate a report on the entries. 如果没有新条目,则转到页面以生成有关这些条目的报告。

When a file is uploaded, it stores the new entries. 上传文件后,它将存储新条目。 In .nessus files, sometimes there is more than one host with entries. 在.nessus文件中,有时有多个主机和条目。 That changes the json structure and the function needs to iterate a little differently. 这就改变了json结构,该函数需要进行一些不同的迭代。 The following function is how those entries are stored. 以下功能是这些条目的存储方式。 This is important as this is where the weird behavior originates (I think) 这很重要,因为这是奇怪行为的起源(我认为)

function parsePluginNumbers(json){

    var pluginNumbers = []

    //Let's check the number of hosts
    var hostLength = json['NessusClientData_v2']['Report'].ReportHost.length

    if (hostLength != undefined) {
        for (var i = 0; i < hostLength; i++) { //Since there is more than 1, need to iterate over each host to find the findings.
            var item_length = json['NessusClientData_v2']['Report'].ReportHost[i].ReportItem.length
            for (var t = 0; t < item_length; t++) { //Iterate through each finding on each host
                if (json['NessusClientData_v2']['Report'].ReportHost[i].ReportItem[t].risk_factor != 'None') { 
                    var newEntry = json['NessusClientData_v2']['Report'].ReportHost[i].ReportItem[t].pluginID
                    if (pluginNumbers.indexOf(newEntry) == -1) {
                        pluginNumbers.push(newEntry)
                    }
                    else {
                        continue
                    }
                } else {
                    continue
                }
            }
        }
    } else {
        var item_length = json['NessusClientData_v2']['Report']['ReportHost'].ReportItem.length
        for (var t = 0; t < item_length; t++) { //Iterate over findings 
                if (json['NessusClientData_v2']['Report']['ReportHost'].ReportItem[t].risk_factor != 'None') { 
                    var newEntry = json['NessusClientData_v2']['Report']['ReportHost'].ReportItem[t].pluginID
                    if (pluginNumbers.indexOf(newEntry) == -1) {
                        pluginNumbers.push(newEntry)
                    }
                    else {
                        continue
                    }
                } else {
                    continue
                }
        }
    }
    return pluginNumbers    
}

Once those plugins are stored. 一旦这些插件被存储。 Another function is called to look if those results are in the mongodbinstance. 调用另一个函数来查看这些结果是否在mongodbinstance中。 In this function, those plugins are in an array "pluginsTotal". 在此功能中,这些插件位于“ pluginsTotal”数组中。

    function queryForNewResultsInANessusFile(pluginsTotal, collectionname, filename){ //function to call mongodb query and send results to parseNewFindings and parseOldFindings.
    var db = req.db;
    var collection = db.get(collectionname);

    collection.find({ 'PluginNumber' : { $in: pluginsTotal }}, 'FindingTitle FindingDescription Remediation Mitigation SeeAlso PluginFamily PluginNumber CVE Risk -_id', function(error, result){
        var newPluginArray = parseOutFindingNumbersInMongoDB(result, pluginsTotal);


//IF statements go here with specific redirects as needed to check if there are new values not in the repo
}

During this collection.find call, there is a function parseOutFindingNumbersInMongoDB that is called to determine if there are plugins in the .nessus results file that are not in the repo. 在此collection.find调用期间,有一个函数parseOutFindingNumbersInMongoDB被调用,以确定.nessus结果文件中是否存在不在存储库中的插件。 It compares the results from collection.find and pluginsTotal (generated from the first function) and returns an array of the new plugins that are not in the repo. 它比较来自collection.find和pluginsTotal(从第一个函数生成)的结果,并返回不在仓库中的新插件的数组。 The function details are below: 功能详细信息如下:

function parseOutFindingNumbersInMongoDB(repoResults, reportPlugins) {

    for (var i = 0; i < repoResults.length; i++){
        var index = reportPlugins.indexOf(repoResults[i].PluginNumber);
        if (index != -1) {
            reportPlugins.splice(index, 1);
            }
        else {
            continue
            }
    }
    return reportPlugins
}

Now to my question --- When I upload a .nessus file with more than one host, parseOutFindingNumberInMongoDB always returns empty even though there are new entries. 现在我的问题---当我上载具有多个主机的.nessus文件时,即使有新条目,parseOutFindingNumberInMongoDB始终返回空。 What gives? 是什么赋予了? Is it the way I parse out the numbers to begin with in the parsePluginNumbers function or is because it is called in the collection.find synchronous function (This seems unlikely as if there is one host, it returns the new plugin values as I want)? 是我解析parsePluginNumbers函数开头的数字的方式,还是因为它是在collection.find同步函数中调用的(这似乎不太可能,因为好像有一个主机,它会根据需要返回新的插件值) ? Any thoughts/ideas/review would be much appreciated as I cannot figure out what is wrong. 任何想法/想法/评论将不胜感激,因为我无法弄清楚出了什么问题。 I have checked the data types within the array before being passed into the functions and they all match up. 在传递给函数之前,我已经检查了数组中的数据类型,它们都匹配。

It's always returning an empty array because every element in the array matches the condition to be spliced. 它总是返回一个空数组,因为数组中的每个元素都匹配要拼接的条件。

You first retrieve elements that have a PluginNumber in pluginTotal array with this filter { $in: pluginsTotal } 首先,使用此过滤器{ $in: pluginsTotal }检索在pluginTotal数组中具有PluginNumber元素

collection.find({ 'PluginNumber' : { $in: pluginsTotal }}, 'FindingTitle FindingDescription Remediation Mitigation SeeAlso PluginFamily PluginNumber CVE Risk -_id', function(error, result){
    var newPluginArray = parseOutFindingNumbersInMongoDB(result, pluginsTotal);
}

Then you remove all elements that have a PluginNumber in pluginTotal 然后,删除在pluginTotal中具有PluginNumber所有元素

    var index = reportPlugins.indexOf(repoResults[i].PluginNumber);
    if (index != -1) {
        reportPlugins.splice(index, 1);
    }

So the result is always an empty array. 因此,结果始终是一个空数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM