0

I am new to Ionic / Firebase and I try to update fields via a form. 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 ?

1
  • does this writeUserData function really been executed? seems you only declared it Commented Feb 15, 2017 at 6:19

1 Answer 1

1

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

Also function writeUserData isn't necessary in this situation

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"); to receiveNewData

Edited

To be able to catch the changes just call add child_added event listener inside 'accounts/' + userId

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"); }); } 
Sign up to request clarification or add additional context in comments.

7 Comments

Waow ! Thank you ! I can't believe it ! Now when it goes to the "tab.account", the changes are not showing, I have to log out and loggin again to see it. Any idea ?
Thank you for this, however it doesn't seem to work. A typo or something ? I mean it saves etc but it doesn't show up by going to the other page.
I have this error : TypeError: firebase.database(...).ref.child is not a function so we get rid of the action to go back to "tab.account" ? Thanks again for your time you have no idea how frustrated I got with that...
This time no error but still it doesn't change the data. Now that I see that, if I click on other tabs and come back on the tab.account I still don't see it changing (even if it has been changed in the database). Maybe I need something to refresh the data when visiting the page itself ?
I forgot to include the $state.go("tab.account"); hehehe inside child_added event listener
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.