NodeJS处理.nessus文件时,查询MongoDB的数组返回空求助
Hey there! I get it, when you're still getting up to speed with a tech stack, weird little snags like empty arrays can feel super frustrating. Let's walk through how to fix this problem with your .nessus file parser and MongoDB check.
First, Pinpoint Where the Array Goes Empty
An empty array here almost always means your code isn't correctly pulling the target numbers from the .nessus file. Since .nessus files are XML-based, let's start with the parsing and extraction steps:
Verify File Reading & XML Parsing
.nessus files use XML structure, so you're probably using a library likexml2jsorfast-xml-parserto parse it. First, make sure you're reading the file correctly and parsing it without hidden errors. Add debug logs to inspect the parsed output:const fs = require('fs/promises'); const xml2js = require('xml2js'); async function parseNessusFile(filePath) { try { const fileContent = await fs.readFile(filePath, 'utf8'); const parser = new xml2js.Parser({ explicitArray: false }); const parsedData = await parser.parseStringPromise(fileContent); // Print the parsed structure to locate your target numbers console.log('Parsed Nessus Data:', JSON.stringify(parsedData, null, 2)); return parsedData; } catch (err) { console.error('Error reading/parsing file:', err); throw err; } }Look for where your target numbers (like plugin IDs or ports) live in the parsed JSON. For example, plugin IDs are typically under
Report.ReportHost.ReportItem.$.pluginID.Fix Your Number Extraction Logic
Once you know the correct path, make sure you're traversing the structure properly and converting string values to numbers. Common mistakes here include:- Using the wrong field name (e.g.,
pluginIdinstead ofpluginID) - Forgetting to convert string values to numbers with
parseInt()orNumber() - Not handling cases where
ReportHostorReportItemmight be an array (some .nessus files have multiple hosts/items)
Example of a reliable extraction function:
async function extractTargetNumbers(filePath) { const parsedData = await parseNessusFile(filePath); // Handle possible arrays for ReportHost and ReportItem const reportHosts = Array.isArray(parsedData.Report.ReportHost) ? parsedData.Report.ReportHost : [parsedData.Report.ReportHost]; const numbers = reportHosts.flatMap(host => { const reportItems = Array.isArray(host.ReportItem) ? host.ReportItem : [host.ReportItem]; return reportItems.map(item => { const num = parseInt(item.$.pluginID, 10); // Filter out non-numeric values return isNaN(num) ? null : num; }).filter(Boolean); }); console.log('Extracted Numbers:', numbers); // Verify this isn't empty! return numbers; }- Using the wrong field name (e.g.,
Check for Async/Await Mistakes
If you're using callback-based file reading or parsing, you might be trying to use the array before it's been populated. Always useasync/awaitto ensure you wait for the file to be read and parsed before accessing the array. Avoid patterns like this:// ❌ Wrong: Uses callback, array is empty when returned let numbers = []; fs.readFile('file.nessus', (err, data) => { // Parse and assign to numbers here }); // numbers is still empty here!Validate Before MongoDB Query
Right before you run your MongoDB check, log the array to confirm it's not empty. If it's still empty, circle back to the parsing/extraction step—your target data isn't being pulled correctly from the .nessus file. If it has values, then the issue might lie in your MongoDB query logic (but your question states the array is empty, so focus on extraction first).
Quick Recap
- Debug the parsed XML structure to find your target numbers
- Fix extraction logic to handle arrays and convert strings to numbers
- Ensure you're using async/await correctly to wait for file operations
- Log the array at every step to track where it goes empty
You've got this—start with those debug logs, and you'll spot the issue in no time!
内容的提问来源于stack exchange,提问作者ZackSied

