Back

[SOLVED] Last document getting updated

  • 0
  • Databases
  • Web
  • Cloud
Susmita
6 Jun, 2023, 15:11

Just tell me how to get the id of only one document

TL;DR
Issue: When clicking the "update" button, only the last item is getting updated instead of the selected item. Solution: The issue was resolved by using a state variable to point to the selected item. The value was stored in the state and used in the `value` attribute of the input. An `onChange` attribute was added to the input to update the value with the `setState` function. It was suggested to use the `value` attribute instead of the `placeholder` to display the value in the input. Additionally, it was recommended to try a suggestion from another user. The user's code was shared on GitHub and
Susmita
6 Jun, 2023, 15:29

The thing is

Susmita
6 Jun, 2023, 15:29

if delete document is working properly

Susmita
6 Jun, 2023, 15:29

Why edit won't work

Susmita
6 Jun, 2023, 15:34

Hey @safwan Can you a take a look here?

safwan
6 Jun, 2023, 15:36

sure let me check

safwan
6 Jun, 2023, 15:40

@Susmita could you send you updated code? Also please try to enable JS formatting by typing js after the first 3 backticks

Susmita
6 Jun, 2023, 15:56

I don't have nitro to do that. It exceeds text limit and can't be formatted

safwan
6 Jun, 2023, 15:56

okay no worries can you just the send a file or something?

Susmita
6 Jun, 2023, 15:57

code is uploaded on github

Susmita
6 Jun, 2023, 15:57

sharing the links here

Binyamin
6 Jun, 2023, 15:58

This is the problem

If you're using conditional rendering, then, you'll always get the last item as this what that item is pointing to.

To overcome it, you can try different approaches.

  • Change from conditional rendering to conditional visibility (with css and classes)
  • Use useRef to create a new pointer for the data.
safwan
6 Jun, 2023, 17:12

@Susmita I just looked through your code. One thing I want to ask is if this issue happens even after you close the modal, and reopen it?

safwan
6 Jun, 2023, 17:14

For example: You said that when you click the last item, it shows the value of the first item right? What happens when you close that modal and try to edit the last item again?

safwan
6 Jun, 2023, 17:15

Another thing is to try @Binyamin's suggestion.

safwan
6 Jun, 2023, 17:15

Also, why display the value in the placeholder, when you can use the value attribute in the input?

safwan
6 Jun, 2023, 17:17

You can store the value in a state, and use that state in the value attribute. Add an onChange attribute to the input that calls the setState function to update the value seamlessly

Susmita
7 Jun, 2023, 03:15

Yes

Susmita
7 Jun, 2023, 03:16

When I click on the first/second item, the last item comes up. After closing the modal and reopening it or reloading the page and then reopening the modal, same thing happens

Susmita
7 Jun, 2023, 03:16

If I click on the last item, then also the last item comes up which is okay that particular one

Susmita
7 Jun, 2023, 03:17

And every time I hit update button by clicking on any of the edits, the last items gets updated

Susmita
7 Jun, 2023, 05:04

Thanks @Binyamin and @safwan

Susmita
7 Jun, 2023, 05:04

Solved the issue by using a state variable to point the selected item

Susmita
7 Jun, 2023, 05:04

[SOLVED] Last document getting updated

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more