1
votes

I created a login page as a small example. The form is asking the usual things such as username, password etc, it also asks the user to confirm a password, and if the length is different it should throw an error warning the user about the discrepancy.

It is working If I enter the correct password and confirm password length,

It is not working if password is unequal to confirm password and the error looks like this:

Error: Failed to lookup view "/register" in views directory "/home/emanuele/Desktop/cashman-tracker-dashboard/views" at Function.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/application.js:580:17) at ServerResponse.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/response.js:1012:7) at ServerResponse.res.render (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express-ejs-layouts/lib/express-layouts.js:77:18) at /home/emanuele/Desktop/cashman-tracker-dashboard/routes/users.js:30:7 at Layer.handle [as handle_request] (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/layer.js:95:5) at next (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/layer.js:95:5) at /home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:335:12) at next (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:275:10) at Function.handle (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:174:3) at router (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:47:12) at Layer.handle [as handle_request] (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:317:13) at /home/emanuele/Desktop/cashman-tracker-dashboard/node_modules/express/lib/router/index.js:284:7

Below is the code with the related checks:

users.js

const express = require('express');
const router = express.Router();

// Login Page
router.get('/login', (req, res) => res.render('login'));
// Register Page
router.get('/register', (req, res) => res.render('register'));

// Register Handle
router.post('/register', (req, res) => {
    const { name, email, password, password2 } = req.body;
    let errors = [];

    // Check required fields
    if (!name || !email || !password || !password2) {
        errors.push({ msg: 'Please fill in all fields' });
    }
    // Check password
    if (password !== password2) {
        errors.push({ msg: 'Passwords do not match' });
    }
    // Check password length
    if (password.length < 6) {
        errors.push({ msg: 'Password should be at least 6 characters' });
    }
    if (errors.length > 0) {
        res.render('register', {
            errors,
            name,
            email,
            password,
            password2
        });
    } else {
        res.send('pass');
    }
});

module.exports = router;

register.ejs

<div class="row mt-5">
    <div class="col-md-6 m-auto">
        <div class="card card-body">
            <h1 class="text-center mb-3"><i class="fas fa-user-plus"></i>Register</h1>
            <% include ./partials/messages %> //<-- This seems to be the cause of error
            <form action="/users/register" method="POST">
                <div class="form-group">
                    <label for="name">Name</label>
                    <input
                    type="name"
                    id="name"
                    name="name"
                    class="form-control"
                    placeholder="Enter Name"
                    value="<%= typeof name != 'undefined' ? name : '' %>" 
                    />
                </div>

                <div class="form-group">
                    <label for="email">Email</label>
                    <input
                    type="email"
                    id="email"
                    name="email"
                    class="form-control"
                    placeholder="Enter Email"
                    value="<%= typeof email != 'undefined' ? email : '' %>"
                    />
                </div>

                <div class="form-group">
                    <label for="password">Password</label>
                    <input
                    type="password"
                    id="password"
                    name="password"
                    class="form-control"
                    placeholder="Create Pasword"
                    value="<%= typeof email != 'undefined' ? password : '' %>"
                    />
                </div>

                <div class="form-group">
                    <label for="password2">Confirm Password</label>
                    <input
                    type="password"
                    id="password2"
                    name="password2"
                    class="form-control"
                    placeholder="Confirm Pasword"
                    value="<%= typeof email != 'undefined' ? password2 : '' %>"
                />
                </div>
                <button type="submit" class="btn btn-primary btn-block">Login</button>
            </form>
            <p class="lead mt-4">Have An Account? <a href="/users/login">Login</a></p>
        </div>
    </div>
</div>

messages.ejs

<% if(typeof errors != 'undefined'){ %> 
    <% errors.forEach(function(error) { %>
      <%= error.msg %>
    <% }); %> 
<% } %>

If it is useful I am also attaching a print screen of my project tree:

tree

The problem seems to be the following statement below in the register.ejs but I don't know

        <% include ./partials/messages %>

I don't know if the messages.ejs could also be related to the error

What I have done so far:

I looked at different sources, and the official documentation is the one I used. I double checked many times but the error still persists and as soon as I enter a password of length X and confirm password of length X+1 I have that strange error.

I am not sure if the error could be related to the messages.ejs or to the statement <% include ./partials/messages %> or if it could be something else.

Thanks for pointing me in the right direction for solving this problem.

1

1 Answers

1
votes

I've been try your code and it's working fine, the only one line not working is:

<% include ./partials/messages %>

You have to change this line with this code below:

<%- include('partials/messages') %>

I hope it can help you.