When I try to redirect to a new URL with query parameters and then render it, the page fails to display. However, if I remove the extra query parameters, the page renders without issue.
These functions are used to verify user logins and display their profile afterward. The intention is for the login function to pass the user's username into the URL, which will then be retrieved by the profile function to populate the profile Pug template.
exports.loginCheck = async (req, res) => {
let usernameStr = req.body.username;
let passwordStr = req.body.password;
var userFound = await User.findOne()
.where("username")
.equals(usernameStr)
.where("password")
.equals(passwordStr)
.select("username email age password answer1 answer2 answer3")
.then((account) => {
console.log(userFound);
res.redirect(`/profile/${account.username}`);
})
.catch((err) => {
console.log(err);
res.render("login", {
errorMsg: 'Username and/or password is incorrect.'
});
});
}
exports.profile = async (req, res) => {
var param = req.params.userP;
var userFound = await User.findOne()
.where("username")
.equals(param)
.select("username email")
.then((user) => {
res.render('profile', {
username: user.username, //I get an error saying cant read '.username', but it will find the user
email: user.email
})
})
}
The routes:
app.get('/profile/:userP', routes.profile); //this one doesnt render
app.get('/profile', routes.profile); //this one does render
app.post('/profile/login', urlencodedParser, routes.loginCheck); //the post route after users login
All of these routes are meant to render the profile pug template.
I've experimented with params, query strings, adjusting the form routes that post to the profile page, redirecting and rendering in both the login and profile functions. My goal was to send data to the login function via the '/profile/login' route, then redirect to the profile page with the user's details or simply pass the username to the profile page so I could fetch the user's information from my database.