sei-ec-remote / project-2-issues

1 stars 0 forks source link

Problem displaying edited information after edit #168

Closed shaialoni closed 2 years ago

shaialoni commented 2 years ago

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

Yesterday everything worked properly, and I have been testing it since, so not even sure what and when this came to be.

After editing an entry, some pages show the edited info, and some not. The DB show the unedited version.

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

//GET Route - display a calendar entry edit form
router.get('/:calId/:eventId/edit', (req, res) => {
    const {calId} = req.params
    const {eventId} = req.params
    const userInfo = req.session.username
    //find the event we want to edit
    Event.findById(eventId)
        .then(event => {
            //send to edit liquid page for editing, attach CalId, eventId and event data as variables
            res.render('personal/PCeditEvent', {calId, eventId, event, userInfo} )
        })
        .catch(err => console.log(err))    
})

//PUT update route - update the edited calendar entry
//test code
router.put('/:calId/:eventId', (req, res)=> {
    const {calId} = req.params
    const {eventId} = req.params
    //find the event being edited, and assign the edited fields to the event
    Event.findByIdAndUpdate(eventId, req.body)
        .then(event => {
            // save the edited document
            event.save()
            res.redirect(`/personal/${calId}`)       
        })
        .catch(err => console.log(err))
})

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

Updated information isn't showing

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

None - this worked yesterday, and I did not think I changed anything that could caused this behavior

What things have you already tried to solve the problem?

Going back to older commits

Paste a link to your repository here

https://github.com/shaialoni/SEIR_Project_2

kestler01 commented 2 years ago

If the update load issue isn't consistent it's probably a race condition where the redirect and the following query are competing against the other operation : try moving the redirect to another ' then '

I'd also want to console.log out the newly undated event in the promise chain

I think we can safely remove the save here as well since this findByIdAndUpdate function has it built in i believe https://mongoosejs.com/docs/api/model.html#model_Model-findByIdAndUpdate

shaialoni commented 2 years ago

Thank you for clarifying about the save, I am never 100% sure so I am just sprinkling them around it feels...... so like this?

router.put('/:calId/:eventId', (req, res)=> {
    const {calId} = req.params
    const {eventId} = req.params
    //find the event being edited, and assign the edited fields to the event
    Event.findByIdAndUpdate(eventId, req.body)
        .then(event => {
            console.log('new updated event', event)
        })
        .then(event => {
            res.redirect(`/personal/${calId}`)       
        })
        .catch(err => console.log(err))
})
shaialoni commented 2 years ago

The code above gives the same result - data in DB remains unchanged, console log of 'new updated event' shows original unchanged event. In the event's show page, it shows the edited field (title, that's the only field visible in the index), But I have no idea where its pulling that updated field from, since the DB does not reflect the change. The worst part, is that it worked properly yesterday :(

kestler01 commented 2 years ago

hey Shai, have we tried logging out and looking at the caI and event Id here ? also sharing all of the logs when sharing issues is helpful

shaialoni commented 2 years ago

Sorry, forgot to add: The top one is the req.body from the edit form, the bottom is the console log in the first .then of the findByIdAndUpdate. I am not sure what you mean by logging out and looking there?

reqbody [Object: null prototype] { title: 'Testing, changed name', date: 'Fri Jan 06 2023 16:45:00 GMT-0800 (Pacific Standard Time)', hdate: '', category: 'candles', hebrew: 'הדלקת נרות', memo: 'Parashat Vayechi' } edited event { _id: new ObjectId("62d6eab7bf3c83b23d09764c"), title: 'Candle lighting: 4:45pm', date: 2023-01-07T00:45:00.000Z, category: 'candles', hebrew: 'הדלקת נרות', memo: 'Parashat Vayechi', calId: '62d6e6b4f35c19567c510158', createdAt: 2022-07-19T17:32:39.930Z, updatedAt: 2022-07-19T17:32:39.930Z, __v: 0 }

kestler01 commented 2 years ago

thank you ! no worries, by 'logging out and looking there' i mean use console.log() and look at them in the terminal to double check those variables . with my post lunch eyes i see we're destructuring there

shaialoni commented 2 years ago

Issue was with the relationship between models and insertion methods.