0

I created a schema for the supports tickets I have to show on this view but, they are not actually showing. The data is showing in the console so I don't really understand the problem.

const mongosee = require ('mongoose');
const {Schema} = mongosee;

const AssignedTicketSchema = new Schema ({
    name: { type: String, required: true},
    pnumber: { type: String, required: true},
    problem: { type: String, required: true},
    support: {type: String, required: true},
    date: { type: Date, default: Date.now}
});

module.exports = mongosee.model('AssignedTicket', AssignedTicketSchema)

These are the routes


router.post('/tickets/assign-tickets', isAuthenticated, async (req, res) => {
  const {name, pnumber, problem, support}= req.body;
  const newAssignedTicket = new AssignedTicket({name, pnumber, problem, support})
  newAssignedTicket.user = req.user.id;
  await newAssignedTicket.save();
  req.flash('success_msg', 'Ticket assigned succesfully')
  res.redirect('/tickets/assigned-tickets');
});

router.get('/assigned-tickets', isAuthenticated, async (req, res) => {
  const assignedtickets = await AssignedTicket.find({user: req.user.id}).lean().sort({date: 'desc'});
  res.render('tickets/assigned-tickets', {assignedtickets} );
});

And this is the view


<div class="row">
    {{#each assignedtickets}}
    <div class="col-md-3">
        <div class="card">
            <div class="card-body">
                <h4 class="card-name d-flex justify-content-between align-items-center">
                    {{name}}
                </h4>
                <p>{{pnumber}}</p>
                <p>{{problem}}</p>
                <p>{{support}}</p>
            </div>
        </div>
    </div>
    {{else}}
        <div class="card mx-auto">
            <div class="card-body">
                <p class="lead">There are no support tickets yet.</p>
                <a href="/tickets/add" class="btn btn-success btn-block">Create a new support ticket</a>
            </div>
        </div>
    {{/each}}
</div>

1 Answers1

0

From this line

 res.redirect('/tickets/assigned-tickets');

Are you expecting to redirect to this method?

router.get('/assigned-tickets', isAuthenticated, async (req, res) => {
  const assignedtickets = await AssignedTicket.find({user: req.user.id}).lean().sort({date: 'desc'});
  res.render('tickets/assigned-tickets', {assignedtickets} );
});

Seems like the path is not an exact match.

Also, the redirect seems like is not sending the user id, probably you can send it as a parameter?

Check this other answers How do I redirect in expressjs while passing some context?

Express.js: Is it possible to pass an object to a redirect like it is with res.render?

how to pass data and redirect in express

jpganz18
  • 5,508
  • 17
  • 66
  • 115