WDI-SEA / project-2-issues

0 stars 0 forks source link

findAll where using current user's id #14

Closed devinrbopp closed 2 years ago

devinrbopp commented 2 years ago

I am trying to filter a table to only display items with the current user's id.

// GET display all recipients
router.get('/', isLoggedIn, (req,res) => {
    db.recipient.findAll({where: {userId: currentUser.id}}) // THIS IS THE PROBLEM ROW
    .then((recipients) => {
        res.render('recipients/index', {recipients: recipients })
    }).catch(error => {
        console.error()
    })
    // res.render('recipients/recipients')
})

This returns the error ReferenceError: currentUser is not defined.

I can't figure out how to access the id of the user in Javascript, but I figured out how to do so in ejs. What is the best way to access the current user's id (from the users table)?

tkolsrud commented 2 years ago

Is there any way to use a url parameter? i.e. is the browser showing a page that could be specifically dedicated to that user's info?

devinrbopp commented 2 years ago

I edited it a bit! The following controller edit worked:

// GET display all recipients
router.get('/', isLoggedIn, (req,res) => {
    db.recipient.findAll({where: {userId: req.user.dataValues.id}})
    .then((recipients) => {
        res.render('recipients/index', {recipients: recipients })
    }).catch(error => {
        console.error()
    })
    // res.render('recipients/recipients')
})

So now the index page will only display recipients associated with the currently logged in user.