微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

变量未传递给Pug

如何解决变量未传递给Pug

我正在创建一个express.js应用程序作为团队项目的一部分。我是一名javascipt新手,但是最终任务落在了我身上。相关代码的目的是使用一些用户定义的选项,在单击按钮时运行脚本,然后显示一个页面显示在结果页面生成的报告的链接。无论出于何种原因,在应用程序启动后第一次都无法使用此功能,但是如果您返回并重试,它将可以使用。我曾以为有一个同步问题,也许有,但是似乎也存在数组变量未传递给pug的问题。数周来,我一直在桌子上猛撞,并向我的教授(我们俩都不是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)

解决方法

您应该使用$ java -version openjdk version "15" 2020-09-15 OpenJDK Runtime Environment (build 15+36-1562) OpenJDK 64-Bit Server VM (build 15+36-1562,mixed mode,sharing) $ java -classpath . foo.Foo <?xml version="1.0" encoding="UTF-8"?><gazonk:xyzzy xmlns:gazonk="urn:foo:bar"> </gazonk:xyzzy> !我确定fs.readdirSync函数中的fs.readdir用完了您预期的处理顺序:

readFolder
,

尝试更改:

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

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

}

收件人:

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

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

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。