这是我第一次使用JS框架,我试图使用工具栏视图引擎将一个对象数组传递给一个部分视图,但是页面上没有显示任何内容。我用不同的数组在其他页面上做了同样的工作,但是似乎不能处理这些特定的数据,我也不知道为什么。请帮帮忙。
app.post('/building-works', (req, res) => {
let theProjects = shared.projects;
let dataObject = {};
let theArray = [];
const search = req.body.searchWork;
for(let projects of theProjects){
if((search === projects.state)){
dataObject.state = projects.state;
dataObject.lga = projects.lga;
for(let localG of projects.lga){
dataObject.lga = projects.lga;
dataObject.communties = localG.communities;
theArray.push(dataObject);
}
}
for(let lga of projects.lga){
if(search === lga.lgaName){
dataObject.state = projects.state;
dataObject.lga = lga.lgaName;
dataObject.communities = lga.communities;
theArray.push(dataObject);
}
for(let communities of lga.communities){
if(search === communities.community){
dataObject.state = projects.state;
dataObject.lga = lga.lgaName;
dataObject.community = communities.community;
dataObject.building = communities.projects.building;
dataObject.water = communities.projects.water;
theArray.push(dataObject);
}
}
}
}
if(!res.locals.projects) res.locals.projects = {};
res.locals.projects = theArray;
console.log(theArray);
res.render('building-works', {title: 'Building Works'});
});这是部分文件
<div class="flex flex-col justify-center items-center">
<h1 class="text-xl w-3/4 text-atasp-mid-red uppercase">Search Projects</h1>
<form class="mx-auto flex flex-col w-full md:flex-row px-5 py-3 items-center justify-center" action="" method="post">
<input type="text" name="searchWork" placeholder="search by state, LGA, community or project" class="bg-gray-200 w-3/4 outline-none p-2 rounded-t-md md:rounded-t-none md:rounded-l-md">
<button type="submit" class="bg-atasp-light-green rounded-b-md w-3/4 md:w-auto text-white md:rounded-bl-none md:rounded-r-md hover:bg-atasp-dark-green p-2">Search</button>
</form>
</div>
{{#each theArray}}
<div>
<p>state</p>
<p>lga</p>
<p>community</p>
<p>building</p>
</div>
{{/each}}发布于 2021-10-31 11:05:27
在处理程序的res.render部分中,将theArray作为模板数据对象的一部分传递。
...
res.render('building-works', {title: 'Building Works', theArray});
...https://stackoverflow.com/questions/69786204
复制相似问题