WDI-SEA / project-2-issues

0 stars 0 forks source link

Without Nested Route, Redirect Seems Impossible #45

Closed dryutsun closed 2 years ago

dryutsun commented 2 years ago

What's the problem you're trying to solve?

Non-nested routes are making it difficult to access the right information for redirects.

Post any code you think might be relevant (one fenced block per file)

router.delete('/:id', isLoggedIn, (req,res)=> {
    // THIS WORKS BUT 
    db.event.destroy({
        where: {id: req.params.id},
        includes: [db.project]
    })
    .then((deletedEvent) => {
        console.log("You removed", deletedEvent)
        // CANNOT REDIRECT TO PROJECT/:id MAIN PAGE
        res.redirect('/projects')
    })
    .catch(error=>{
        console.error
    })

[mode] event.js

    static associate(models) {
      models.event.belongsTo(models.project)
    }
  };

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

Nothing unexpected really, but some redirects are difficult to accomplish.

What is your best guess as to the source of the problem?

Non-nested routes underdetermine how much information they require to display what they need.

What things have you already tried to solve the problem?

I've tried to rewrite the routes multiple times.

dryutsun commented 2 years ago

Cory helped me out during lunch. Solution: You can store certain information in a nested promise chain -- at a higher scope, you can pass down variables that store information from your db query, and use them at the most inner scope where you render a page.

router.delete('/:id', isLoggedIn, (req,res)=> {
    db.event.findByPk(req.params.id)
    .then(foundEvent => {
        let projectId = foundEvent.projectId
        foundEvent.destroy()

        .then(deletedEvent => {
        console.log("You Removed", deletedEvent)
        res.redirect(`/projects/${projectId}`)
    }) 
    })
    .catch(error=>{
        console.error
    })