I have this following code for import user data from Firebase to my React application :
import React from 'react';
import AuthUserContext from './AuthUserContext';
import withAuthorization from './withAuthorization';
import * as firebase from 'firebase';
import { config, database, db, auth, itembase, } from
'../firebase/firebase';
class Collection extends React.Component {
constructor (props) {
super(props)
this.state = {
collection: []
};
}
//Data from Firebase Database
ponentDidMount() {
var userUid = firebase.auth().currentUser.uid;
const collection = firebase.database().ref(`/users/${userUid}/collection/`)
collection.on('value', snapshot => {
this.setState({
collection: snapshot.val(),
})
})
}
//Remove from user collection
removeToCollection(key, e) {
const userUid = firebase.auth().currentUser.uid;
const item = {
nom: this.state.collection[key].nom,
parution: this.state.collection[key].parution
};
firebase.database().ref(`users/${userUid}/collection/`).remove(item)
}
render(){
const collection= Object.keys(this.state.collection).map(key => {
return (
<div key={key}>
<h3>{this.state.collection[key].nom}</h3>
<p>{this.state.collection[key].parution}</p>
<button className="btn btn-danger" onClick={this.removeToCollection.bind(this, key)}>Remove</button> </div>
)
});
return (
<div>
{collection}
</div>
)
}
}
const authCondition = (authUser) => !!authUser;
export default withAuthorization(authCondition)(Collection);
Like you can see, I have a function for remove data when the user click on the remove button. My problem is that when I click the button, I get an error message that appears
And here is what my database looks like:
Thank a lot in advance for your help !
I have this following code for import user data from Firebase to my React application :
import React from 'react';
import AuthUserContext from './AuthUserContext';
import withAuthorization from './withAuthorization';
import * as firebase from 'firebase';
import { config, database, db, auth, itembase, } from
'../firebase/firebase';
class Collection extends React.Component {
constructor (props) {
super(props)
this.state = {
collection: []
};
}
//Data from Firebase Database
ponentDidMount() {
var userUid = firebase.auth().currentUser.uid;
const collection = firebase.database().ref(`/users/${userUid}/collection/`)
collection.on('value', snapshot => {
this.setState({
collection: snapshot.val(),
})
})
}
//Remove from user collection
removeToCollection(key, e) {
const userUid = firebase.auth().currentUser.uid;
const item = {
nom: this.state.collection[key].nom,
parution: this.state.collection[key].parution
};
firebase.database().ref(`users/${userUid}/collection/`).remove(item)
}
render(){
const collection= Object.keys(this.state.collection).map(key => {
return (
<div key={key}>
<h3>{this.state.collection[key].nom}</h3>
<p>{this.state.collection[key].parution}</p>
<button className="btn btn-danger" onClick={this.removeToCollection.bind(this, key)}>Remove</button> </div>
)
});
return (
<div>
{collection}
</div>
)
}
}
const authCondition = (authUser) => !!authUser;
export default withAuthorization(authCondition)(Collection);
Like you can see, I have a function for remove data when the user click on the remove button. My problem is that when I click the button, I get an error message that appears
And here is what my database looks like:
Thank a lot in advance for your help !
/users/${userUid}/collection/
).set(null);
– Shubham Agarwal Bhewanewala
Commented
Aug 1, 2018 at 11:40
The error es from:
firebase.database().ref(`users/${userUid}/collection/`).remove(item)
It looks like you're trying to pass in the item to remove. That's not how the remove
method works.
The remove
method removes all data at the location you call it on. If you pass in a parameter to remove
, Firebase expects that parameter to be a callback function that it calls after the delete pletes (or fails).
So you'll need to create the plete path to the item to remove:
let key = "-LlouZxkW1N3Llt6h5nm"
firebase.database().ref(`users/${userUid}/collection/${key}`).remove()
If you don't know the key of the item to remove, you will first need to execute a query to find that item and determine its key.