Javascript 变量未传递给帕格

Javascript 变量未传递给帕格,javascript,node.js,express,pug,Javascript,Node.js,Express,Pug,我正在创建一个express.js应用程序,作为团队项目的一部分。我是javascipt新手,但任务最终落在了我身上。相关代码的目的是在单击按钮时使用一些用户定义的选项运行脚本,然后显示新页面并显示结果页面上生成的报告的链接。无论出于何种原因,这在应用程序启动后的第一次都不会起作用,但如果您返回并重试,它会起作用。我原以为有一个同步问题,可能有,但似乎也有一个数组变量并没有传递给帕格的问题。几周来,我一直把头撞在桌子上,向我的教授寻求帮助(我们都不是CS人),但运气不佳。请帮忙 以下是文件开头的

我正在创建一个express.js应用程序,作为团队项目的一部分。我是javascipt新手,但任务最终落在了我身上。相关代码的目的是在单击按钮时使用一些用户定义的选项运行脚本,然后显示新页面并显示结果页面上生成的报告的链接。无论出于何种原因,这在应用程序启动后的第一次都不会起作用,但如果您返回并重试,它会起作用。我原以为有一个同步问题,可能有,但似乎也有一个数组变量并没有传递给帕格的问题。几周来,我一直把头撞在桌子上,向我的教授寻求帮助(我们都不是CS人),但运气不佳。请帮忙

以下是文件开头的应用程序变量、配置等:

// index.js

/**
 * Required External Modules
 */

const express = require("express");
const path = require("path");
const shell = require("shelljs");
const fs = require("fs");






/**
 * App Variables
 */

const app = express();
const port = process.env.PORT || "8000";
var ipAddresses;
var ipAddressesLink;








/**
 *  App Configuration
 */

app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "reports/html")));

//code to make html forms work
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
以下是一直让我失望的相关路线:

//Run script when post is rec'd from root and send to results page
app.post("/", (req, res) => {
    //take values and create complete command for Astrum script
    var commandString = 'bash /home/astrum/Main/Astrum.sh -s ' + req.body.speed + ' -h ' + req.body.host + ' -u ' + req.body.username + ' -p ' + req.body.password;
    var pathToReports = './reports/html';
  
    runScript(commandString);

    readFolder(pathToReports);
    
    renderPage();
    
    
    //Iterate thru filenames to create arrays for links and link labels
    function readFolder(pathValue) {

        fs.readdir(pathValue, (err, files) => {

            console.log(files)
                
            //variable & method for links to html records pages
            ipAddressesLink = files;

            console.log(ipAddressesLink);
            
            //variable and method to remove file extension for link labels in pug
            ipAddresses = files.map(removeExtension);

            
        });

    }

    //function to remove last five characters of each element
    function removeExtension(value) {

        return value.substring(0, value.length - 5);

    };

    //function to render the page
    function renderPage() {

        res.render("results", {ipAddressesLink, ipAddresses, title: 'Results'});

    }

    //function to execute command in shell
    function runScript(value) {

        shell.exec(value);

    }


    //show array on console for debugging
    console.log("type of record is: " + typeof ipAddressesLink);
    console.log(ipAddressesLink);
    console.log(ipAddresses);

    res.end();
});
下面是抛出错误的结果页面的pug模板,显然是正在进行的工作:

extends layout

block layout-content
  
  div.View
    
    div.Message
      
      div.Title
        
        h1 Astrum Network Analysis
      
        div.Body          
          
          div.multiple-group
          
            h3 Heading
            select(id='whitelist', name='whitelist' size='6' multiple)
              option(value="volvo") Volvo
              option(value="saab") Saab
              option(value="fiat") Fiat
              option(value="audi") Audi
              option(value="bmw") BMW


          div.form-group

            label(for='whitelistButton')
            input(type='submit' value='Whitelist Ports')


          h3 Hosts Found:  
            
            ul

              each val, index in ipAddressesLink

                li: a( href = val ) #{ipAddresses[index]}
下面是我收到的错误消息:

TypeError: /home/astrum/Main/astrumApp/views/results.pug:36
    34|             ul
    35| 
  > 36|               each val, index in ipAddressesLink
    37| 
    38|                 li: a( href = val ) #{ipAddresses[index]}
    39| 

Cannot read property 'length' of undefined
    at eval (eval at wrap (/home/astrum/Main/astrumApp/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:93:32)
    at eval (eval at wrap (/home/astrum/Main/astrumApp/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:116:4)
    at template (eval at wrap (/home/astrum/Main/astrumApp/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:119:7)
    at Object.exports.renderFile (/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:452:38)
    at Object.exports.renderFile (/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:442:21)
    at View.exports.__express [as engine] (/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:491:11)
    at View.render (/home/astrum/Main/astrumApp/node_modules/express/lib/view.js:135:8)
    at tryRender (/home/astrum/Main/astrumApp/node_modules/express/lib/application.js:640:10)
    at Function.render (/home/astrum/Main/astrumApp/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/home/astrum/Main/astrumApp/node_modules/express/lib/response.js:1012:7)
TypeError:/home/astrum/Main/astrumApp/views/results.pug:36
34 | ul
35| 
>36 |每个val,ipAddressesLink中的索引
37| 
38 | li:a(href=val)#{ipaddress[index]}
39| 
无法读取未定义的属性“length”
评估时(在包装时评估(/home/astrum/Main/astrumApp/node_modules/pug runtime/wrap.js:6:10),:93:32)
评估时(在包装时评估(/home/astrum/Main/astrumApp/node_modules/pug runtime/wrap.js:6:10),:116:4)
at模板(在包装时进行评估(/home/astrum/Main/astrumApp/node_modules/pug runtime/wrap.js:6:10),:119:7)
在Object.exports.renderFile(/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:452:38)
在Object.exports.renderFile(/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:442:21)
at View.exports.u express[as engine](/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:491:11)
在View.render(/home/astrum/Main/astrumApp/node_modules/express/lib/View.js:135:8)
在tryRender(/home/astrum/Main/astrumApp/node_modules/express/lib/application.js:640:10)
位于Function.render(/home/astrum/Main/astrumApp/node_modules/express/lib/application.js:592:3)
在ServerResponse.render(/home/astrum/Main/astrumApp/node_modules/express/lib/response.js:1012:7)
尝试更改:

 //function to render the page
function renderPage() {

    res.render("results", {ipAddressesLink, ipAddresses, title: 'Results'});

}
致:


您应该使用
fs.readdirSync
!我确信
readFolder
函数中的
fs.readdir
已超出您预期的处理顺序:

function readFolder(pathValue) {

    //variable & method for links to html records pages
    ipAddressesLink = fs.readdirSync(pathValue);

    //variable and method to remove file extension for link labels in pug
    ipAddresses = ipAddressesLink.map(removeExtension);

}

你确定文件不是空的吗?但是,
console.log(ipaddress-slink)
打印了什么?不幸的是,这不起作用,但感谢您的帮助。搞定了!非常感谢。
function readFolder(pathValue) {

    //variable & method for links to html records pages
    ipAddressesLink = fs.readdirSync(pathValue);

    //variable and method to remove file extension for link labels in pug
    ipAddresses = ipAddressesLink.map(removeExtension);

}