Isabel
Isabel

Reputation: 55

firebase can't delete/update from first item on the list (React js)

React.useEffect(() => {
        const fetchData = async () => {
            const db = firebase.firestore();
            
            db.collection('users')
                .onSnapshot(snapshot =>
                    setSpells(
                        snapshot.docs.map(doc => ({
                            id: doc.id,
                            ...doc.data()
                        }))
                    )
                );
            
        };
        fetchData();
    }, []);

mapped out like this

{spells.map(spell => (
                    <li key={spell.id}>
                        <SpellInput spell={spell} />
                    </li>
                ))}

update and deleted like this

const onUpdate = () => {
        const db = firebase.firestore();
        db.collection('users')
            .doc(spell.id)
            .set({ ...spell, name });
    };

const onDelete = () => {
    const db = firebase.firestore();
    db.collection('users').doc(spell.id).delete();
};

can't seem to delete or edit the first item on the list on firebase. Can someone explain why?

here's my repo https://github.com/isabelchua/CRUD

Upvotes: 1

Views: 166

Answers (1)

Isabel
Isabel

Reputation: 55

I found the solution

id: doc.id, ...doc.data()

should be

...doc.data(), id: doc.id,

Upvotes: 1

Related Questions