Home > Software design >  Mongoose schema not showing in view
Mongoose schema not showing in view

Time:02-23

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 >
    {{#each assignedtickets}}
    <div >
        <div >
            <div >
                <h4 >
                    {{name}}
                </h4>
                <p>{{pnumber}}</p>
                <p>{{problem}}</p>
                <p>{{support}}</p>
            </div>
        </div>
    </div>
    {{else}}
        <div >
            <div >
                <p >There are no support tickets yet.</p>
                <a href="/tickets/add" >Create a new support ticket</a>
            </div>
        </div>
    {{/each}}
</div>

CodePudding user response:

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

  • Related