gpt4 book ai didi

javascript - 删除在 React 和 Firebase 中不起作用

转载 作者:行者123 更新时间:2023-12-03 07:30:00 26 4
gpt4 key购买 nike

我创建了一个应用程序,其中用户数据在前端使用reactjs注册,并在后端使用firebase。我可以将数据保存到 firebase。我可以编辑和删除数据,但无法将编辑的数据保存到 firebase,而且我的 deleteUser 事件也不会删除已删除的节点。可能是什么原因?

这是我的代码

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

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>
);
}
}

正在删除。但我怎样才能保存编辑过的数据呢?

最佳答案

尝试:

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

关于javascript - 删除在 React 和 Firebase 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35843834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com