使用reactjs将编辑后的数据保存到firebase
Save edited data to firebase using reactjs
我正在创建一个应用程序,其中在前端使用 reactjs 并使用 firebase 作为后端来注册用户数据。我可以将数据保存到 firebase。我可以编辑和删除数据,但无法将编辑后的数据保存到 firebase。我应该如何编辑数据?我用了child_changed
但还是不行。
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
);
const foundEmail = _.find( this.state.userInfo, user =>
user.email === oldUser.email
);
const foundUsername = _.find( this.state.userInfo, user =>
user.username === oldUser.username
);
const foundPassword = _.find( this.state.userInfo, user =>
user.password === oldUser.password
);
console.log('foundUser', foundUser);
console.log('foundUser.name',foundUser.name);
foundUser.name = newUser.name;
foundContact.contact = newUser.contact;
foundAddress.address = newUser.address;
foundEmail.email = newUser.email;
foundUsername.username = newUser.username;
foundPassword.password = newUser.password;
}
根据文档:https://www.firebase.com/docs/web/guide/
var ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog");
//suppose you have similar kind of data in your data base which was created like this
var usersRef = ref.child("users");
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});
//then to change data
usersRef.child("alanisawesome").set({
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
});
usersRef.child("gracehop").set({
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
});
//remember that using 'set' causes overwrite data at that localtion to avoid this you can also use 'update' . like if your db looks like this
{
"users": {
"alanisawesome": {
"date_of_birth": "June 23, 1912",
"full_name": "Alan Turing",
"nickname": "A Machine"
},
"gracehop": {
"date_of_birth": "December 9, 1906",
"full_name": "Grace Hopper",
"nickname": "A Grace"
}
}
}
//then you can do this
usersRef.update({
"alanisawesome/nickname": "Alan The Machine"
"gracehop/nickname": "Amazing Grace"
});
您可以获取保存在 loadData 函数中的 ID。所以你可以这样做
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
);
const foundEmail = _.find( this.state.userInfo, user =>
user.email === oldUser.email
);
const foundUsername = _.find( this.state.userInfo, user =>
user.username === oldUser.username
);
const foundPassword = _.find( this.state.userInfo, user =>
user.password === oldUser.password
);
console.log('foundUser', foundUser);
console.log('foundUser.name',foundUser.name);
foundUser.name = newUser.name;
foundContact.contact = newUser.contact;
foundAddress.address = newUser.address;
foundEmail.email = newUser.email;
foundUsername.username = newUser.username;
foundPassword.password = newUser.password;
}
const editUserRef = new Firebase('https://***.firebaseio.com/users').child(oldUser.id);
console.log('edit',editUserRef);
editUserRef.update(foundUser);
我正在创建一个应用程序,其中在前端使用 reactjs 并使用 firebase 作为后端来注册用户数据。我可以将数据保存到 firebase。我可以编辑和删除数据,但无法将编辑后的数据保存到 firebase。我应该如何编辑数据?我用了child_changed
但还是不行。
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
);
const foundEmail = _.find( this.state.userInfo, user =>
user.email === oldUser.email
);
const foundUsername = _.find( this.state.userInfo, user =>
user.username === oldUser.username
);
const foundPassword = _.find( this.state.userInfo, user =>
user.password === oldUser.password
);
console.log('foundUser', foundUser);
console.log('foundUser.name',foundUser.name);
foundUser.name = newUser.name;
foundContact.contact = newUser.contact;
foundAddress.address = newUser.address;
foundEmail.email = newUser.email;
foundUsername.username = newUser.username;
foundPassword.password = newUser.password;
}
根据文档:https://www.firebase.com/docs/web/guide/
var ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog");
//suppose you have similar kind of data in your data base which was created like this
var usersRef = ref.child("users");
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});
//then to change data
usersRef.child("alanisawesome").set({
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
});
usersRef.child("gracehop").set({
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
});
//remember that using 'set' causes overwrite data at that localtion to avoid this you can also use 'update' . like if your db looks like this
{
"users": {
"alanisawesome": {
"date_of_birth": "June 23, 1912",
"full_name": "Alan Turing",
"nickname": "A Machine"
},
"gracehop": {
"date_of_birth": "December 9, 1906",
"full_name": "Grace Hopper",
"nickname": "A Grace"
}
}
}
//then you can do this
usersRef.update({
"alanisawesome/nickname": "Alan The Machine"
"gracehop/nickname": "Amazing Grace"
});
您可以获取保存在 loadData 函数中的 ID。所以你可以这样做
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
);
const foundEmail = _.find( this.state.userInfo, user =>
user.email === oldUser.email
);
const foundUsername = _.find( this.state.userInfo, user =>
user.username === oldUser.username
);
const foundPassword = _.find( this.state.userInfo, user =>
user.password === oldUser.password
);
console.log('foundUser', foundUser);
console.log('foundUser.name',foundUser.name);
foundUser.name = newUser.name;
foundContact.contact = newUser.contact;
foundAddress.address = newUser.address;
foundEmail.email = newUser.email;
foundUsername.username = newUser.username;
foundPassword.password = newUser.password;
}
const editUserRef = new Firebase('https://***.firebaseio.com/users').child(oldUser.id);
console.log('edit',editUserRef);
editUserRef.update(foundUser);