這是我第一次使用 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}}
uj5u.com熱心網友回復:
在處理程式的 res.render 部分中,將 theArray 作為模板的資料物件的一部分傳遞
...
res.render('building-works', {title: 'Building Works', theArray});
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/343606.html
標籤:javascript 表达 把手.js
