Eval issue; "data is not defined"
originalbluef3x opened this issue · 1 comments
originalbluef3x commented
Hello! I am receiving the error "data is not defined" however, the page is being rendered without any issues.
First my main code is here:
const app = express();
let validUserTokens = {};
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const cookieParser = require('cookie-parser');
app.set('view engine', 'ejs');
app.use(cookieParser());
const path = require('path')
const fs = require('fs')
const generateToken = // Redacted for copyright reasons, I was told.
app.get("/", async (req, res) => {
try {
// Load indexData from JSON file
let indexData = JSON.parse(fs.readFileSync('./pagedata/indexData.json'));
// Render index.ejs template and pass indexData
res.render("index", {
data: indexData // Pass indexData to the template
});
} catch (err) {
console.error('Error loading indexData:', err);
res.status(500).send('Internal Server Error');
}
});
app.get("/edit/:file", (req, res) =>{
try{
res.render(req.params.file, {
editMode: true,
dark: true
})
}catch{
res.sendStatus(401).send("Err.")
}
})
app.get(['/secure/enter', '/login'], (req, res)=>{
if(req.query.render == "NXUSR"){
res.render("loginV2")
}else{
res.render('login')
}
})
app.post('/login/backend', (req, res) =>{
let loginsFile = JSON.parse(fs.readFileSync('./logins.json'));
let responseData = req.body;
//console.log(responseData);
// Check if the request body contains username and password
if (!responseData || !responseData.username || !responseData.password) {
return res.status(400).send("Username and password are required.");
}
// Check if there are login entries
if (loginsFile.length === 0) {
return res.status(401).send("No users found in the system.");
}
// Check if the provided credentials match any entry in loginsFile
const user = loginsFile.find(user => user.username === responseData.username && user.password === responseData.password);
if (user) {
// Credentials are valid
let newToken = generateToken(128, "method")
res.cookie("token", newToken)
console.log(user)
validUserTokens[newToken] = {
username: user.username,
id: user.id,
name: user.name
}
return res.redirect('/app');
} else {
// Credentials are invalid
return res.redirect('/secure/enter/?render=NXUSR')
}
});
app.get('/app', (req, res) =>{
let token = req.cookies['token']
let user = validUserTokens[token]
if(!user || typeof(user) == 'undefined') return res.redirect('/')
res.render('app', {
user
})
})
app.get('/assets/:asset', (req, res) =>{
res.sendFile(path.resolve(`./assets/${req.params.asset}`))
})
app.get('*', (req, res) =>{
res.render("index", {
dark: true
})
// res.se("<html><p>The Requested Resource could not be found. <a href="/">Head Back</a></p></html>")
})
process.on('unhandledRejection', function(){
})
process.on('uncaughtException', function(){
})
app.listen(1080, function(){console.log("Running a marathon on port 1080ti")})`
My JSON file is here
{
"title": "REDACTED ENTIRELY",
"underConstruction": "Hello and welcome! This site is under construction, if you have any suggestions on how we can make this site better, tell us ",
"navBar": [
{
"text": "Events",
"url": "#"
},
{
"text": "Donate",
"url": "/venmo"
},
{
"text": "Contact",
"url": "mailto:contactREDACTED@REDACTED.com"
},
{
"text": "Login",
"url": "/secure/enter"
},
{
"text": "Venmo",
"url": "/venmo"
}
],
"carousel": [
{
"imgSource": "",
"id": "crescendo",
"textData": [
{
"text": "2024 Competition: Crescendo"
},
{
"text": "Learn More",
"url": "/2024/game"
}
]
}
]
}
I do not believe my actual page is configured wrong, If you need it, I will send it in a follow up but it is too long to send now
Thank you!
originalbluef3x commented
Issue resolved, res.render("*") was for some reason still firing.