D
D
Daniel161rus2019-06-27 12:14:01
React
Daniel161rus, 2019-06-27 12:14:01

How to remove user from firebase database by uid?

It is necessary that when clicking on 'delete', the user is deleted from the firebase by uid.

import React from 'react';
import { Table,Tag,Spin,Popconfirm } from 'antd';

import { withFirebase } from '../Firebase';

class TableOffice extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      loading: false,
      users: [],
    };
  }

  componentDidMount() {
    this.setState({ loading: true });

    this.props.firebase.users().on('value', snapshot => {
      const usersObject = snapshot.val();
      const dataSource = Object.keys(usersObject).map(key => ({
        ...usersObject[key],
        uid: key,
      }));
      this.setState({
        users: dataSource,
        loading: false,
      });
    });
  }

  render(){

    const { users, loading } = this.state;
        
    const columns = [
      {
        title: 'Имя',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: 'Фамилия',
        dataIndex: 'surname',
        key: 'surname',
      },
      {
        title: 'E-mail',
        dataIndex: 'email',
        key: 'email',
      },
      {
        title: 'Телефон',
        dataIndex: 'phone',
        key: 'phone',
      },
      {
        title: 'Статус',
        key: 'check',
        dataIndex: 'check',
        render: status => status ? <Tag color = 'green'>На работе</Tag> : <Tag color = 'red'>Отсутствует</Tag>
      },
      {
        title: 'Действие',
        dataIndex: 'action',
        render: (text, record) =>
          this.state.users.length >= 1 ? (
            <Popconfirm okText="Да" cancelText= "Отмена" title="Точно удалить?" onConfirm={this.onRemoveMessage}>
              <a href='javascript:;'>Удалить</a>
            </Popconfirm>
          ) : null,
      },
    ];
    return(
      <div>
        <Table
        columns={columns}
        dataSource={users}
         />
      </div>
    );
  }
}

export default withFirebase(TableOffice);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
seropaski, 2019-07-03
@Daniil161rus

const userObj = this.props.firebase.database().ref('users/ID');
userObj.remove() // it return Promise

More details in the doc
https://firebase.google.com/docs/reference/js/fire...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question