javascript - 变量没有传递给 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();
});
这是引发错误的结果页面的哈巴狗模板,显然正在进行中:
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)
解决方案
你应该使用fs.readdirSync
!我确定fs.readdir
in readFolder
function 已超出您预期的流程顺序:
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);
}
推荐阅读
- python - Python:如何从 for 循环中的列表中仅减去相应的值
- ffmpeg - FFMPEG 图像到视频的反向序列与其他过滤器
- scala - 由于阶段故障而中止作业:ShuffleMapStage 20(在 data_prep.scala:87 处重新分区)失败了最大允许次数:4
- python - 使用 concurrent.futures.ThreadPoolExecutor() 作为执行者:...不等待
- node.js - 需要使用 Electron JS 与现金抽屉等硬件设备交互
- excel - Excel VBA:用范围交换列
- opengl-es - 使用双 FBO 在 vs 多 FBO 之间切换
- ruby-on-rails - 如何使用较少的数据库事务创建/更新多条记录(避免循环)。?
- html - 如何在边框表格单元格的右下角创建一个垃圾方块?
- java - 无法通过更改其布尔条件来停止线程中的 while 循环