Reducer日志已“删除”,但仍存在于Firestore数据库中...?

时间:2019-04-04 08:02:53

标签: reactjs firebase redux google-cloud-firestore action

'id'通过动作和reducer发送并注销'deleted',但实际上并未从firestore数据库中删除...

客户列表:

class Clients extends Component {

  handleClick = (id) => {
    // e.preventDefault();
    this.props.deleteClient(id)
  }

  render() {
    const {clientList} = this.props
    return (
      <div className="container mt-5 text-center">
        <h2>Here Are Your List of Clients...</h2>
        {clientList && clientList.map(client => {
          return(
            <div key={client.id}>
              <div className="my-2">
                Client Name: {client.name} | Client Price: ${client.price}
                <button className="ml-2" onClick={() => {this.handleClick(client.id)}}>x</button>
              </div>      
            </div>

          )
        })}
        <AddClient/>
      </div>
    )
  }
}

const mapStateToProps = (state) => {
  return {
    clientList : state.firestore.ordered.clientList,
  }
}

const mapDispatchToProps = (dispatch) => {
  return{
    deleteClient : (id) => dispatch(deleteClient(id)) 
  }
}

export default compose(
  connect(mapStateToProps, mapDispatchToProps),
  firestoreConnect([
    {collection: 'clientList', orderBy: 'name'},
  ])
)(Clients)

动作:

export const deleteClient = (id) => {
    return(dispatch, getState, {getFirestore, getFirebase}) => {
        const firestore = getFirestore();
        firestore.collection("clientList").doc(id).delete().then(() => {
            dispatch({type: 'DELETE CLIENT'})
        }).catch((err) => {
            dispatch({type: 'DELETE CLIENT ERROR', err})
        });

    }     
}

让我知道您是否需要其他任何代码或信息。 ps,注销到控制台没有错误。

1 个答案:

答案 0 :(得分:0)

尝试一下。 .doc(id),因为id必须是字符串。

我认为您不需要在/中使用doc

Check the api.

export const deleteClient = (id) => {
    console.log(id);
    return(dispatch, getState, {getFirestore, getFirebase}) => {
        const firestore = getFirestore();
        firestore.collection('clientList').doc(id).delete().then(() =>{
            dispatch({type: 'DELETE CLIENT'})
        }).catch((err) => {
            dispatch({type: 'DELETE CLIENT ERROR', err})
        })
    }     
}