变量未传递给 Pug
2020-10-26
692
我正在创建一个 express.js 应用程序作为团队项目的一部分。我是 javascipt 新手,但这个任务最终落到了我身上。相关代码的目的是在单击按钮时使用一些用户定义的选项运行脚本,然后显示新页面并显示已在结果页面上生成的报告的链接。无论出于何种原因,这在应用程序启动后第一次不起作用,但如果您返回并重试,它会起作用。我原以为存在同步问题,可能存在,但似乎还存在数组变量未传递给 pug 的问题。几个星期以来,我一直在为此而头疼,并向我的教授寻求帮助(我们都不是计算机科学人员),但没有成功。请帮忙。
这是文件开头的应用程序变量、配置等:
// 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)
2个回答
您应该使用
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);
}
tom
2020-10-26
尝试将:
//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'});
}
Eduardo Conte
2020-10-26