I've looked at previous posts and the documentation and other resources, but I just cannot fiugre out how to add an entry to my firebase database when a button is clicked. The button calls writeNewPost(). Here is what I tried
<button type="submit" class="btn btn-light" onclick="writeNewPost()">Submit</button>
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
<script>
var config = {
apiKey: MYAPIKEY,
authDomain: MYDOMAIN,
databaseURL: MYURL,
projectId: MYPROJECT,
storageBucket: STORAGEBUCKET,
messagingSenderId: SENDERID
};
firebase.initializeApp(config);
</script>
<script>
function writeNewPost() {
var postData = {
"Name": "Brett"
};
var newPostKey = firebase.database().ref().push().key;
var updates = {};
updates['/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
</script>
All I want is to have "Name":"Brett" under the root.
'/' + newPostKeyis the right path...onclick="writeNewPost()"Have you tried to put aconsole.log("CALLED!")inside yourwriteNewPost?