I want to update some data in an object in my firebase database, but have been unsuccessful so far.
I have been following the documentation to the point, but can't seem to get it right.. I'm fairly new to firebase, but this shouldn't be a huge problem.
My structure looks like this.
And this is how i have been trying to solve it:
userRef.on("value", function(snapshot) {
var user = firebase.auth().currentUser;
var key = user.uid;
var getUser = snapshot.val();
var userData = Object.keys(getUser);
var userKey = userData[0];
console.log(userKey);
var db = firebase.database();
var ref = db.ref("sofit-8ad52/profiles");
console.log(key);
var usersRef = ref.child(key);
usersRef.set({
userKey: {
Username: "TEST",
},
});
});
Would love some help on this one.
Related
I would like to save my data like a photo shown below:
this is my code
var databaseRef = firebase.database().ref('Apartment/');
var address = document.getElementById("addr").value;
var uids = firebase.database().ref().child('Apartment').push().key;
var data = {
address: address
}
var updates = {};
updates['/Apartment/' + uids] = data;
firebase.database().ref().update(updates);
I'm able to retrieve the data but it's only showing the data of the last user in the database. The users are listed by their uid. Below are the code and screenshot of the data from firebase. Help me out, guys!
var database = firebase.database().ref('users');
database.on("child_added", function(snapshot) {
var snapVal = snapshot.val();
displayName.innerHTML = snapVal.mUsername;
console.log(snapVal);
var badge = document.createElement('span');
badge.textContent = 'view details';
badge.setAttribute('class','badge');
var list = document.createElement('a');
list.textContent = snapVal.mUsername;
list.setAttribute('href', '#');
list.setAttribute('class', 'list-group-item');
patientList.appendChild(list);
list.appendChild(badge);
var userIcon = document.createElement('i');
userIcon.setAttribute('class','far fa-user');
list.insertBefore(userIcon, list.childNodes[0])
},
function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
You should refer to the specific user's node to retrieve the information.
Your code should look like:
var userRef = firebase.database().ref(`users/${user_node_key}`); // user_node_key should be the node containing the user info.
You should be able to get the node key using the current user's object.
and then call once on the userRef if you just need to get the detail once or on if you need to listen all the changes on that user.
The code should look like:
userRef.once('value').then(function(snapshot) {
//use snapshot to get the users data
});
I am creating a webapp for a class assignment and would like to create a login with only a username. I've tried pushing just a username to the database, but it gets pushed as a child to the connection ID in Firebase. Here is the Javaascript I have created so far:
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
var database = firebase.database();
var connectionsRef = database.ref("/connections");
var userRef = database.ref("/users")
var connectedRef = database.ref(".info/connected");
connectedRef.on("value", function (snap) {
// If they are connected..
if (snap.val()) {
// Add user to the connections list.
var con = connectionsRef.push(true);
// Remove user from the connection list when they disconnect.
con.onDisconnect().remove();
}
});
var database = firebase.database();
var username = "";
$("#submit").on("click", function (event) {
event.preventDefault();
username = $("#username").val().trim();
userRef.push({
username: username
});
$("#login-screen").attr("class", "hidden");
});
I plan on allowing the user to maintain their stats after leaving the page, so I figured assigning them to a username would be the way to go. If there are better ways to go about this, please let me know.
You can make a separate node in your database with name usernames and store all the usernames of your users in there.
And for checking a right login, you can run a check in that child looking for the username your user entered, and if it matches with any of them, you can grant them login access.
Attaching a value observer to a list of data will return the entire list of data as a single snapshot which you can then loop over to access individual children.
ref.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
// ...
});
});
Read more about this in docs here.
You can also use on('value', ...) as you would for a child event.
I tried to solve it like this:
How to pull out data from firebase into html page - Stackoverflow
But I couldn't get it to work.
My html looks where I'll put the information from Firebase looks this in javascript:
var createTable = "<table><thead><tr id='keysRow'></tr></thead>";
var endHead = "<tbody><tr id='valuesRow'></tr></tbody></table>";
//More code that's connecting to each other and displays it in a div.
//F12 is showing me that this works.
Firebase.js:
var firebase = require('firebase');
firebase.initializeApp(config);
var v = firebase.database();
var users = firebase.database().ref("users");
users.orderByKey().once('child_added', function(snapshot){
snapshot.forEach(function(childsnapshot){
var key = childsnapshot.key(); <---------error
var data = childsnapshot.val();
$('#keysRow').append('<th>' + key + '</th>');
$('#valuesRow').append('<td>' + data + '</td>');
});
});
config is just the link to the firebase. I have no problem using the config to write in the firebase.
The error is telling me: "childsnapshot.key is not a function"
Also my firebase has 10 different values I need to get.
Change this:
var key = childsnapshot.key();
to this:
var key = childsnapshot.key;
check this link to see what has changed in Firebase 3.x: https://firebase.google.com/support/guides/firebase-web
I have the following code where I want to update some simple values in my Firebase database using the JavaScript, Web SDK.
However, it doesn't run/update my database. What's wrong here?
var dbRef = firebase.database().ref().child('feeds').child(selectedFeed).child('audio');
var uid = dbRef.push().key;
var data = {
"downloadURL": uploadTask.snapshot.downloadURL,
"fileName": file.name,
"timeStamp": selectedDateUnix
};
var updates = {};
updates["mostRecentKey"] = uid;
updates[uid] = data;
dbRef.update(updates).then(function(){
//success
alert("Successfully Uploaded. This is now available to be listened to by your users.");
}).catch(function(error){
//failure
alert(error.message);
});
While it doesn't ever specify in the Firebase docs and actually I felt indicated it wasn't required; You MUST cast your data. For example:
This does not work. Directly accessing the variable.
var myNum = 10;
var data = {};
data["myNum"] = myNum;
This does work, but doesn't allow a dynamic use of a var.
var data = {};
data["myNum"] = 10;
Finally, this works and was my solution using a variable reference. Cast your data.
var myNum = 10;
var data = {};
data["myNum"] = Number(myNum);