[SOLVED]Look for the answer below
Having problem with deleting an object from firebase database, have experience with that but this time it is not working:
action:
export const firebase_db = firebase.database().ref();
export function deleteData(key) {
return dispatch => firebase_db.child(key).remove();
}
reducer:
case DELETE_DATA:
return _.omit(state, action.payload);
basic_list.js:
clickHandler() {
this.props.deleteData();
}
And button:
<td><button
onClick = {this.clickHandler()}
className="btn btn-outline-secondary btn-danger">
Delete
</button></td>
I should access key of firebase object and it is in this.props.data[post], but when I do that like this:
clickHandler() {
this.props.deleteData(this.props.data[post]);
}
it says ReferenceError: post is not defined, then when do this in onClick:
onClick = {this.clickHandler(this.props.data[post])}
with:
clickHandler() {
this.props.deleteData();
}
I have error Reference.child failed: First argument was an invalid path = "undefined". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
and then when I try with backtick, as example:
clickHandler(key) {
this.props.deleteData(`key`);
}
with
onClick = {this.clickHandler(this.props.data[post])}
I don't have any error, but nothing is happening...
maping looks like this:
renderData(DataRender) {
if (_.isEmpty(this.props.data)) {
return <td>Loading...</td>
} else {
return Object.keys(this.props.data).map((post, key) => (
<tr key={key} post={post} id={key}>
<td>{this.props.data[post].adress}</td>
button is in this map function, you can see how am I accessing value of adress, [post] is a hash of that object...
And if anyone asks, I have binded clickHandler in constructor,
Thank you!
EDIT: So now my action looks like this:
export function deleteData(key) {
return dispatch => firebase_db.child(`${key}`).remove();
}
but when I try inserting hash with this.props.data[post] it says ReferenceError: post is not defined
postis an object? Your best bet is to look at whatthis.props.datahas (property-wise) and then double-check your accessor. To access a property likethis.props.data[something],[something]must be a string or resolve to a stringthis.props.data[post], but in the same function it says post is not defined (can get specific object value, make Link to every object but can't put it in the clickHandler), thats why I don't understand what is happening