Search inside objects javascript - javascript

I'm experimenting on login with firebase. I can make an account, and it'll store additional information too. The problem is retrieving this information.
I can get it using:
usersRef.on("value", function(snapshot) {
console.log(snapshot.val())
}, function (errorObject) {...});
When I do this I get two things (because I have two accounts):
-JrrzEOqZQU0HVeYVXCm: Object, has uid: "simplelogin:21" inside of it
-JrrzgOgQY2z6tNYN0BY: Object, has uid: "simplelogin:22" inside of it
Inside of the second object is the info that I need:
I received simplelogin:22 from the login.
Is there a way I can search inside of the objects to their uid, and get the rest of the information stored inside of that object?
Here's a fiddle

var thisAuthData = authData.uid;
//console.log(authData)
var usersRef = new Firebase("https://fiery-heat-xxx.firebaseio.com/users");
usersRef.on("value", function(snapshot) {
for(var amount in snapshot.val()){
console.log(snapshot.val()[amount].uid);
//here some if statement thingy's to check with your authData but you can do that yourself I guess ;)
}

Related

how to check if value exists in realtime database firebase

I have created a realtime database on firebase and having no issues adding and removing data in tables etc.
I currently have it setup like this:
So my goal is to check if a given value is inside my database currently.
for example, I would like to check if 'max' is currently a username in my database.
var data = db.ref('loginInfo/');
data.on('value', function(snapshot) {
_this.users = snapshot.val()
});
That is how I get all the values, it is saved into _this.users
(How do i check if a value is inside this object, i am making a login program)
if i console.log the object, this is what I see:
image
If you want to check if a child node exists under loginInfo where the username property has a value of max, you can use the following query for that:
var ref = db.ref('loginInfo/');
var query = ref.orderByChild('username').equalTo('max');
query.once('value', function(snapshot) {
console.log(snapshot.exists());
});
I'd also recommend reading the Firebase documentation on queries, as there are many more options.

How to fix 'Array.push is not a function' error in Javascript

Im trying to create a small database with localstorage for a small project ( just to emulate a login ).
It's supposed to open two prompts to ask username and password and then store it in a Array of Objects. What happens is that I iniciate the first element of the Array with an Object of an admin and when after inserting the second user (it lets me add one more after the admin) it says that the Users(Array).push is not a function.
var name = prompt('Insert your name');
var password = prompt('Insert your password');
if(localStorage.length!==0){
Users=localStorage.getItem('Users');
}else {
var Users = [{
name:'admin',
password:'admin'
}];
}
var person = {
name:name,
password:password
}
Users.push(person);
localStorage.setItem('Users',Users);
Its supposed to store users and passwords as object inside the array as I load the page over and over again.
Thank you in advance for anyone willing to help
localStorage stores key/value pairs, but the value is stored as strings, not other data structures.
localStorage.setItem('Users', JSON.stringify(Users));
and then when you get it:
Users = JSON.parse(localStorage.getItem('Users'));
You are having an error because when you get it from localStorage it's not an Array.

Finding a register by field

I have my database in firebase as you can see here https://snag.gy/bhopOk.jpg
The user scans a product and brings you the number of this product, which is put in an input, then when you click on search you must bring the description and the value of that item and show it somewhere in the front
This is my frontend
How would the function or query be so that when the person click on search brings me the requested data? I know it's a simple query but I do not have it very clear I'm new to ionic
I tried to do this to see if I returned something in the console but nothing
findproduct(){
var ref = firebase.database().ref("/productos/");
ref.orderByChild("Producto").equalTo(1706).on("child_added", function(snapshot) {
console.log(snapshot.key);
});
}
You can write your function as follows. However, note that query.once() is asynchronous and returns a Promise. So you have to take that into account when you call it.
findproduct(productID) {
var query = firebase.database().ref("productos").orderByChild("Producto").equalTo(productID);
query.once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
console.log(childKey);
var childData = childSnapshot.val();
console.log(childData);
});
});
}
When you just want to query once, use the once() method: it "Listens for exactly one event of the specified event type ('alue' in this case), and then stops listening". See the documentation here.
When using the on() method, you are constantly listening to data changes at the location defined by the query. See the doc here.

How to get the value of children in Firebase Javascript?

This is my Firebase Database:
I need the URLs of the images that are associated alongside the unique random name generated by the push method. Is there any way I could do that? Also, there must exist a better way of sending data. Please, let me know. Thanks.
imgRef.on('value', function(snapshot) {
console.log(snapshot.val());
});
This is, as expected, returning the entire JSON object. I need the URL.
This is the most basic way to show the list of image URLs:
var rootRef = firebase.database.ref();
var urlRef = rootRef.child("user1/DAA Notes/URL");
urlRef.once("value", function(snapshot) {
snapshot.forEach(function(child) {
console.log(child.key+": "+child.val());
});
});

Firebase set new unique ID

I'm new to firebase and practicing creating a ToDo application. So far this is my code:
Code:
var getUserRef = firebase.database().ref('users');
getUserRef.on('child_added', function(data) {
var name = data.child("name").val();
var email = data.child("email").val();
$('#loadData').append(`<tr><td>${name}</td><td>${email}</td><td>Edit Remove</td></tr>`);
});
function addUser() {
var database = firebase.database();
var name = $('#name').val();
var email = $('#email').val();
var newID = 4; // How can this be incremented?
database.ref('users/' + newID).set({
name: name,
email: email
});
}
Data Structure:
As you can see, everytime I create a new user, I am manually changing the newID variable.
My problem is I don't want to change it everytime I insert a new user. I want it to aumotically increment or generate a random unique ID.
Is there a way to do this in google firebase?
What you have here is a list of items. If you have such a list, it is common to use Firebase's push() method to add new items to it. This will generate a chronological key that is guaranteed to be unique across all clients, even in cases where some clients may have temporarily lost their network connection while they're adding data. These push IDs are not as readable as your array indices, but they are the recommended way of adding items to a list.
That said; you're not dealing with any list here, it is a list of users. If you're using Firebase Authentication to manage the user accounts and handle authentication, the users come with a build in id called uid. In such a case it makes more sense to store the items under their natural key, so under their uid in this case.
Yes there is.
Here is a code from firebase docs of an example of Push().
var messageListRef = firebase.database().ref('message_list');
var newMessageRef = messageListRef.push();
newMessageRef.set({
'user_id': 'ada',
'text': 'Example of text.'
});
You can find all this information about other methods here :
https://firebase.google.com/docs/reference/js/firebase.database.Reference

Categories

Resources