How do you get specific firebase data without knowing the unique id? - javascript

I have some data in my firebase database that I want to retrieve. I did one big push so that all instances would have a unique id. I know want to retrieve this data
I know I can do this:
var gymData = firebase.database().ref('gymData/previousWorkouts')
but when I get returned data like this:
how the hell am I suppose to access that id without knowing it?
for example a user of my app is going to search for an exercise and update it. they search by name, how do I find that id and look inside there? I don't get it :/

firebase.database().ref('gymData/exrcises').orderByChild('name').equal("barbell bench press").once('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;//this is id
});
});

Related

Appmaker: How can I get the ID of an item i just created, on client side?

So my latest problem is the following:
I store an id(a foreign key) in a custom property. I want to set the Foreign Key field of an item I just created, to the stored custom property.
I am trying to achieve this in a server script, which looks like this:
function setFKforCustomer(customerID, companyID)
{
var query = app.models.Customer.newQuery();
query.filters.Id._equals(customerID);
var records = query.run();
records[0].Company_fk=companyID;
}
I want to call this function from a client side script, which has to know the customer's ID, the id of the item I just created. I am not entirely sure I am approaching the problem from the right angle, so I am open to other ideas.
Thank you in advance (Markus)!
The question of how to manage relationships in App Maker is probably one of the most asked questions. The official documentation for your specific use case is located here https://developers.google.com/appmaker/models/relations#server_script. The answer would be as follows:
function setFKforCustomer(customerID, companyID)
{
var customerRecord = app.models.Customer.getRecord(customerID);
var companyRecord = app.models.Company.getRecord(companyID);
customerRecord.YourCompanyRelationName = companyRecord; //the field in this case is not Company_fk but whatever the name is of the relation you set up in AM
app.saveRecords([customerRecord]);
}

how do i grab push ID from database firebase

how do i grab the PUSH ID generated from firebase push method. I need to store this key in my App.js file for queries in the future. im not sure how to grab this key when the user is already logged in. I posted a pic of what im reffering to, to clarify
To get the pushid based on the email provided, then try the following:
firebase.database().ref().child("users").orderByChild("email").equalTo(yourEmail).on("value", function (snapshot) {
snapshot.forEach(function(childSnapshot) {
var randomKey=childSnapshot.key;
});
});
The snapshot is at the node users then you loop inside the keys and retrieve them using the key property
https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#key

How to query from firebase realtime database?

How to get all the books of a specific author from my database?
Here is a snapshot of my database, i want to get "Colson Whitehead"
for web development, javascript.
To get all books by author Colson Whitehead, you do a query like this:
var query = firebase.database().ref("books").orderByChild("author").equalTo("Colson Whitehead");
query.on("value", function(snapshot) {
snapshot.forEach(function(bookSnapshot) {
console.log(bookSnapshot.key+": "+bookSnapshot.val());
});
})
This callback will get called initially and then every time something about the books by Colson Whitehead changes. If you only want the initial call, use once instead of on.

Querying firebase database to retrieve user name

I am working on firebase for the first time. I am confused in querying the database. I have two objects on the database, one is the Auth and second one is the Chats. In Auths I have number of UID(user id) nodes, each of these nodes have their respective username. What I am trying to do is, I want to query that Auth object and get all the usernames which is equal to the one I take from a user through input box. In my sql it will be simple as SELECT * FROM auth WHERE username = userInputValue. I need same query like this, Below is what I have done so far.
var _firbaseDB = firebase.database(),
_firebaseAuthRef = _firbaseDB.ref("Auth/");
$("body").on("submit",".search-from",function(e){
e.preventDefault();
var ev = e.target,
_allUsers = $(ev).serializeArray()
_user = _allUsers[0].value;
_firebaseAuthRef.equalTo(_user).on("child_added",function(res){
console.log(res.val());
})
})
You were almost there. What's missing is that you need to specify which attribute you're querying on:
_firebaseAuthRef.orderByChild("n").equalTo(_user).on("child_‌​added",function(res)‌​{
console.log(res.val());
})
More info on the Firebase Database Documentation.

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());
});
});

Categories

Resources