Passing const between classes

Viewed 34

I want to pass all the errors from create.ejs class to even-routes.js class so I can print them in the browser so I made const errors in create.ejs class and I called it in even-routes.js. However, it looks like even-routes.js doesn't recognize it. What should I do in this case?

create.ejs

 <%- include('../partials/header.ejs') -%>
        <%- include('../partials/navbar.ejs') -%>
        
        
            <div class="container">
                <div class="row mt-4">
        
                    <div class="col-md-9 offset-md-3">
                        <hr>
                        <h1 class="display-4">Add a new event</h1>
                        <% errors.forEach((err)=> {%>
                            <%= err.msg %>
                            <%})%>
                        <form action="/events/create" method="post">
        
        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" name="title" id="title" class="form-control">
        </div>
        
        <div class="form-group">
            <label for="description">Description</label>
            <textarea name="description" rows="4" class="form-control"></textarea>
        </div>
        
        <div class="form-group">
            <label for="location">Location</label>
            <input type="text" name="location" id="location" class="form-control">
        </div>
        
        <div class="form-group">
            <label for="date">Date</label>
            <input type="date" name="date" id="date" class="form-control">
        </div>
        
        <div class="mt-2 form-group" >
            <button type="submit" class="btn btn-primary">Create</button>
        </div>
                    </form>
                </div>
            </div>
        </div>
            <%- include('../partials/footer.ejs') -%>

even-routes.js

//save event to DB
router.post('/create', [
    check('title').isLength({min: 5}).withMessage('Title should be more than five'),
    check('description').isLength({min: 5}).withMessage('description should be more than five'),
    check('location').isLength({min: 5}).withMessage('location should be more than five'),
    check('date').isLength({min: 5})

] ,(req,res)=> {
    const errors = validationResult(req)

    if (!errors.isEmpty()){

        res.render('event/create', {
            errors: errors.array()
        })
    }
    
    else {
        let newEvent = new Event({
            title: req.body.title,
            description: req.body.description,
            location: req.body.location,
            date: req.body.date,
            created_at: Date.now()
        })
    
        newEvent.save((err)=>{
            if(!err){
                console.log("ADDED")
                res.redirect('/events')
            }
            else{
                console.log("error")
            }
        })    }
  })

enter image description here

0 Answers
Related