简体   繁体   English

删除在React和Firebase中不起作用

[英]Delete is not working in react and firebase

I have created an app where the user data are registered using reactjs in frontend and firebase as a backend. 我创建了一个应用程序,其中在前端和firebase中使用reactjs来注册用户数据。 I could save data to firebase. 我可以将数据保存到Firebase。 I could edit and delete the data but could not saved edited data to firebase and also my deleteUser event wont remove the deleted node. 我可以编辑和删除数据,但无法将编辑后的数据保存到Firebase,并且我的deleteUser事件也不会删除已删除的节点。 What might be the reason ? 可能是什么原因?

Here is my code 这是我的代码

const userInfo = [
      { id:1, name:'',contact:'',address:'',email:'',username:'',password:'' }
    ];

export default class App extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
        userInfo:userInfo
      }
    this.userRef = new Firebase('https://***.firebaseio.com/users/');
    this.createUser = this.createUser.bind(this);
    this.saveUser = this.saveUser.bind(this);
    this.deleteUser = this.deleteUser.bind(this);

  }

  loadData(){
    this.userRef.on('value', snap => {
      let userInfo = [];
      snap.forEach( data => {
        let userData = data.val();
        userData.id = data.name();
        userInfo.push(userData);
      });
      this.setState({ userInfo });
    });

    this.userRef.on('child_removed', (dataSnapshot) =>{
        delete this.state.userInfo[dataSnapshot.key()];
        this.setState({ userInfo: this.state.userInfo });
    });

  }

  componentDidMount(){
    this.loadData();
  }

  createUser(user){
    this.userRef.push(user);
  }

  saveUser(oldUser, newUser){
    console.log('olduser',oldUser);
    console.log('newuser', newUser);
    // TODO - optimize this code
    const foundUser = _.find( this.state.userInfo, user =>
        user.name === oldUser.name
    );
    const foundContact = _.find( this.state.userInfo, user =>
        user.contact === oldUser.contact
    );
    const foundAddress = _.find( this.state.userInfo, user =>
        user.address === oldUser.address
    );
    foundUser.name = newUser.name;
    foundContact.contact = newUser.contact;
    foundAddress.address = newUser.address;
  }

  deleteUser(id){
    console.log(id);
    const dltUserRef = new Firebase('https://***.firebaseio.com/users').child(id);
    console.log('dlt',dltUserRef);
    dltUserRef.remove();
  }

  render() {
    return (
      <div>
        <FormBox createUser = { this.createUser } />
        <UsersList
            userInfo = { this.state.userInfo }
            saveUser = { this.saveUser }
            deleteUser = { this.deleteUser } />
      </div>
    );
  }
}

user-list-item.js user-list-item.js

import React, { Component } from 'react';

export default class UserslistItem extends Component {
    constructor(props){
        super(props);

        this.state = { isEditing: false };

        this.onEditClick = this.onEditClick.bind(this);
        this.onCancelClick = this.onCancelClick.bind(this);
        this.onSaveClick = this.onSaveClick.bind(this);
    }

    onEditClick(){
        this.setState({ isEditing: true });
    }

    onCancelClick(){
        this.setState({ isEditing: false });
    }

    onSaveClick(event){
        event.preventDefault();
        const oldUser = [
            {
                name:this.props.name,
                address:this.props.address,
                contact:this.props.contact
            }
         ];
        const newUser = [
            {
                name: this.refs.editName.value,
                address: this.refs.editAddress.value,
                contact: this.refs.editContact.value
             }
        ];
        // console.log('old user is', oldUser);
        // console.log('new user is', newUser);
        this.props.saveUser( ...oldUser, ...newUser );
        this.setState({ isEditing: false });
    }


        return(
                <div className = "buttons">
                    <button className="btn btn-warning" onClick = { this.onEditClick }>Edit</button>
                    <button className="btn btn-danger" onClick = { this.props.deleteUser.bind(this, this.props.id) }>Delete</button>
                </div>
            );

    }

    render() {
        return (
            <div className = "container" >
                <div className="row">
                    <div className="col-md-4">
                        <div className="card card-block">
                             { this.renderUserSection() }
                            { this.renderActionSection() }
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

Deleting is working now. 现在正在删除中。 But how could i save edited data ? 但是如何保存已编辑的数据?

try: 尝试:

this.userRef.on('child_removed', (dataSnapshot) => {
  delete this.state.userInfo[dataSnapshot.val().id];
  this.setState({ userInfo: this.state.userInfo });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM