[英]Data not updating in Firebase and Ionic
I am new to Ionic / Firebase and I try to update fields via a form. 我是Ionic / Firebase的新手,我尝试通过表单更新字段。 Everything works, no error, all console log show up what needed but the data are not being updated in the database.
一切正常,没有错误,所有控制台日志都显示了所需的内容,但是数据库中的数据并未更新。
Here is my controller : 这是我的控制器:
var database = firebase.database();
var userId = firebase.auth().currentUser.uid;
var nameInput = document.querySelector('#name');
var descriptionInput = document.querySelector('#description');
var saveButton = document.querySelector('#save');
saveButton.addEventListener("click", function() {
var name = nameInput.value;
var description = descriptionInput.value;
function writeUserData(name, description) {
firebase.database().ref('accounts/' + userId).set({
name: name,
description: description,
});
}
$state.go("tab.account");
});
Any idea ? 任何想法 ? Or maybe a better method to simply update firebase's database via a form when the user is logged in ?
还是一种更好的方法,可以在用户登录时通过表格简单地更新Firebase的数据库?
Seems you didn't really don't know the real significance/uses of function yet about when to use it 似乎您还真的不知道功能的真正意义/用途,何时使用它
Well it's because you wrap it inside writeUserData
and which is you didn't event execute/call this function 好吧,因为您将其包装在
writeUserData
,并且没有事件执行/调用此函数
Also function writeUserData
isn't necessary in this situation 同样在这种情况下,不需要函数
writeUserData
so remove function it 所以删除功能吧
var database = firebase.database();
var userId = firebase.auth().currentUser.uid;
var nameInput = document.querySelector('#name');
var descriptionInput = document.querySelector('#description');
var saveButton = document.querySelector('#save');
receiveNewData();
function receiveNewData() {
// check if there's new data added
firebase.database().ref('accounts/' + userId).on('child_added', function(msg) {
var data = msg.val();
// your new data
console.log(data);
$state.go("tab.account");
});
}
saveButton.addEventListener("click", function() {
var name = nameInput.value;
var description = descriptionInput.value;
firebase.database().ref('accounts/' + userId).set({
name: name,
description: description,
});
});
You just transfer $state.go("tab.account");
您只需转移
$state.go("tab.account");
to receiveNewData
receiveNewData
Edited
已编辑
To be able to catch the changes just call add child_added
event listener inside 'accounts/' + userId 为了能够捕捉到更改,只需在'accounts /'+ userId内添加
child_added
事件监听器
function receiveNewData() {
firebase.database().ref('accounts/' + userId).on('child_added', function(msg) {
var data = msg.val();
// your new data
console.log(data);
$state.go("tab.account");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.