Echoesong / Break-The-Ice

0 stars 2 forks source link

One to Glow on! #32

Open sakimastringer opened 1 year ago

sakimastringer commented 1 year ago

Glow Snippet - One piece of code your team is especially proud of - provide comments.

As a group, our commaDores Team is most proud of our Delete Respond Sub-Document code. We had little to no resistance creating the code for the Delete Icebreaker Document. We thought the Sub-Document code would be easy. SURPRISE!!! It wasn't as easy as we suspected. We tried writing various code that failed. Then we tried to to exhaust all of our resources by:

After we chased our tails for a decent amount of time...we decide to use one of out tokens in the debug channel. Josh directed our attention to the Notion Hunger for more page. On that page we are able to figure out a successful solution to our function code for Delete Respond Sub-Document.

This was a great Glow moment for our team for a few reasons:

function destroy(req, res){
    let foundIcebreaker

    Icebreaker.findOne({'responses._id': req.params.id})
    .then( function(icebreaker) {
        return foundIcebreaker = icebreaker
    })
    .then( function() {
        foundIcebreaker.responseCount--
        return foundIcebreaker.responses.remove(req.params.id)
    })
    .then( function(){
        return foundIcebreaker.save()
    })
    .then( function() {
        return res.redirect(`/icebreakers/${foundIcebreaker._id}`)
    })
    .catch( function(err){
        console.log(err)
        res.redirect('/icebreakers')
    })
}
maker-jws commented 1 year ago

Really nice work here team; your explanation emphasizes your independence, problem-solving strategies, and the collaborative efforts your team used before seeing escalated help.

To improve the code, I would three changes:

  1. Removing the responseCount decrementor / incrementor, as it will be implemented through your field's array property's built in length, following the update.
  2. Refactor to use async/await within a try/catch block for a more compact/modern promise implementation.
  3. Stretch goal - if an error occurs, redirect the user to an error page for server errors or explore 'flash' messages to provide on page error handling for user input errors (trying to delete a post that does not belong to the user, etc.)