Muditx
Muditx

Reputation: 355

React firebase delete operation

Creating a simple application to learn firebase, wanting to delete a particular item in a list. So, decided to use this approach

  const handleDeletion = (name) => {
        db.collection('users').doc(`"${name}"`).delete().then(() => {
            console.log("document deleted successfully");
        }).catch((err) => {
            console.log("error:",err)
        })
    }
//don't mind syntax,below is my idea to delete the item
 <div>
            {docs && docs.map(doc => (
                <div key={doc.id}>
                    <h4>{doc.name}:{doc.password}</h4>                
                    <button onClick={() => handleDeletion(doc.name)}>Delete</button>
                </div>
            ))}
            
        </div>

I took this info from firebase web docs, but when runnning this does not delete the respective h4 and also the document but console says document deleted successfully.

Upvotes: 0

Views: 119

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

The problem most probably comes from the fact that you do db.collection('users').doc(`"${name}"`). You should not use double quotes when declaring your template literal: db.collection('users').doc(`${name}`).

You can see the problem by doing:

const name = "abcd";
console.log(`"${name}"`) // => "abcd" instead of abcd

Upvotes: 1

Related Questions