Witam, mam dziwny problem. Mam następujący kod w EJS
if(!tournamentsList){%>
Brak zaplanowanych turniejów
<%}else if(tournamentsList.length!==0){
const today = new Date();
let count=0;
for(i=0; i<tournamentsList.length ; i++){
console.info(tournamentsList[i].date)
const date = tournamentsList[i].date;
const peaple = getPeapleCount(tournamentsList[i].name);
console.info wyświetla prawidłowo:
2021-04-07T16:47:00.000Z
Jednak następna linijka już się sypie, a co więcej błąd mówi o undefined, gdzie przecież na górze w kodzie widać że jest na to warunek (i przechodzi bo jak widać mogę wyświetlić). Błąd:
TypeError: C:\Users\User\Desktop\bilard\views\tournaments.ejs:42
40| </header>
41| <br>Poniżej znajduje się lista zaplanowanych na najbliższy czas turniejów. Organizowane są one w naszym klubie. Osoby chętne do uczestnictwa, muszą jedynie zapisać się, a samą płatność dokonać gótówką na miejscu w dniu turnieju.<br><br>
>> 42| <%
43| function checkSubscribe(tournamentname,username){
44| let found=false;
45| if(typeof usersInTournamentsList != "undefined"){
Cannot read property 'date' of undefined
at eval (eval at compile (C:\Users\User\Desktop\bilard\node_modules\ejs\lib\ejs.js:662:12), <anonymous>:75:51)
at tournaments (C:\Users\User\Desktop\bilard\node_modules\ejs\lib\ejs.js:692:17)
at tryHandleCache (C:\Users\User\Desktop\bilard\node_modules\ejs\lib\ejs.js:272:36)
at View.exports.renderFile [as engine] (C:\Users\User\Desktop\bilard\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\Users\User\Desktop\bilard\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\User\Desktop\bilard\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\User\Desktop\bilard\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\User\Desktop\bilard\node_modules\express\lib\response.js:1012:7)
at ServerResponse.res.render (C:\Users\User\Desktop\bilard\node_modules\express-flash\lib\express-flash.js:29:16)
at C:\Users\User\Desktop\bilard\server.js:80:17
Czy to jest jakiś błąd w ejs? Bo zauwazyłem (bo mam już to drugi raz), że jak zmienię nazwę tej zmiennej przy wysyłaniu i odnowa pozmieniam wszędzie na tą drugą nazwę, to się wszystko naprawia - tak jakby gdzieś były błędne dane przechowywane i potrzebny by był jakiś twardy rebuild, żeby wszystkie pliki się odświeżyły, da się tak w node? Reset kompa też nie pomaga, a nie chce mi się, naprawiać takich rzeczy w taki dziwny sposób, że od nowa zmienną robie i wszedzie nazwe zmieniam.
Próbowałem, też zamiast !tournamentsList to typeof == undefined i też nic to nie zmienia.
Edit: wstawiam cały skrypt w ejs:
<%
function checkSubscribe(tournamentname,username){
let found=false;
if(typeof usersInTournamentsList != "undefined"){
for(i=0 ; i<usersInTournamentsList.length ; i++){
if(usersInTournamentsList[i].tournamentname===tournamentname){
if(usersInTournamentsList[i].username===username){
found=true;
}
}
}
}
return found;
}
function getPeapleCount(tournamentname){
let count=0;
if(typeof usersInTournamentsList != "undefined"){
for(i=0 ; i<usersInTournamentsList.length ; i++){
if(usersInTournamentsList[i].tournamentname===tournamentname)
count++;
}
}
return count;
}
function changeDateNumber(number){
if(number<10)
number="0"+number;
return number;
}
if(messages.subscribed){ %>
<div class="articlecenter">
<b> <%= messages.subscribed %> </b>
</div>
<% }
if(!tournament){%>
Brak zaplanowanych turniejów
<%}else if(tournament.length!==0){
const today = new Date();
let count=0;
for(i=0; i<tournament.length ; i++){
console.info(tournament)
const date = tournament[i].date;
const peaple = getPeapleCount(date);
if(date < today){
count++;%>
<div class = "tournament">
<b><%= tournament[i].names %></b><br>
<b><%= changeDateNumber(date.getDate())+"."+changeDateNumber(date.getMonth())+"."+date.getFullYear()+" "+changeDateNumber(date.getHours())+":"+changeDateNumber(date.getMinutes())%></b><br><br>
<b>Poziom: </b><%=tournament[i].level%><br>
<img height="70" src="img/<%=tournament[i].level%>.png"/><br>
<b>Koszt wstępu:</b> <%= tournament[i].cost %><br>
<b>Nagroda:</b> <%= tournament[i].reward %><br><br>
<b>Liczba osób:</b> <%=peaple%>/<%= tournament[i].maxpeaple%>
<%if(peaple>=tournament[i].maxpeaple || checkSubscribe(tournament[i].name,user.username)){ %>
<br><br>
<form action="/turnieje" method="POST">
<input type="hidden" name = "tournamentname" value="<%= tournament[i].name %>"/>
<input type="submit" value="Zapisz się"/>
</form>
</div>
<%}
}
}
if(count===0){ %>
Brak zaplanowanych turniejów
<%}
} %>