I want to retrieve item id and get it to variable using items other value. according to this image ("get id where Brand="Manchee" and ItemName="Cream Cracker" and SubCategory="100g" ")
how write this function
Here you can try this solution.I think you wanted to query the database so you can fire query on your database based on your variable value.Just pass the key and value in this query and you can have all the data reside in the table which falls within this query.
var ref = firebase.database().ref("items");
ref.orderByChild("itemName").equalTo('Manchee').once("value", (items : any)=> {
console.log(items.key);
console.log(items.val());
let itemArray: any = [];
items.forEach((item) => {
itemArray.push({
key: item.key,
});
});
this.items = itemArray;
console.log("Prescription Data: ", itemArray);
});
Related
I am using the below code which fetches the data of database users. I want to ascending order it by age.
//fetch all data
var starCountRef = firebase.database().ref('users/');
starCountRef.on('value', function(snapshot) {
console.log(snapshot.val());
});
Attached screenshot for reference:
How can I get the minimum age record to be on top?
Edit :: 1
age is a number in database
I tried order by child with below code but still no expected result.
var starCountRef = firebase.database().ref('users/');
starCountRef.orderByChild("age").on('value', function(snapshot) {
console.log(snapshot.val());
});
result is still the same
You should update structure of the users collection so that the age property is a number (not a string). So you'll simply need to use orderBy. Something like this:
const starCountRef = firebase.database().ref('users').orderByChild("age");
starCountRef.on('value', function(snapshot) {
console.log(snapshot.val());
});
At the moment, the age property is a string so sorting will not behave as expected ("11" would come before "3" or "4")
I have not been able to find a reference in the documentation on how to get a document reference when you know the id of it in firebase.
I am passing the id to a webpage to lookup a QR code. Rather than storing a secondary unique id for each qrcode document I am relying on the firebase unique id.
Here is the lookup I tried but which seems to fail.
firebase.firestore().collection('cues').doc(id).get().then(function (docsnapshot) {
console.info('About: ' + docsnapshot.get('text'));
});
I was able to get my original code to work with this modification to the query
firebase.firestore().collection('cues').doc(id).get().then((doc) => {
... and then just use doc.get("field") to get values form my document
you can access to data like this:
const { id } = docsnapshot
const data = docsnapshot.data()
const myDoc = { id, ...data }
myDoc.text
myDoc.anything...
I was integrate firebase with ionic3 angularjs, adding data successfully like
var fireData =
{
userid : '1122233',
questionId : '18022',
answerId : '25633',
points : '2'
}
//Add
this.sample.list('paperCode').push(fireData);
Now I want to update like below mentioned image
If you don't know the key, first query the data with the field you know then iterate through the result and get the key. Then you can perform update.
Try
updateFunction(questionId){
this.sample.list('/paperCode', ref => ref.orderByChild('questionId').equalTo(questionId)).snapshotChanges()
.subscribe(actions => {
actions.forEach(action => {
// here you get the key
console.log(action.key);
this.sample.list('/paperCode').update(action.key, { points: 10 });
});
});
}
I hope your question id is unique, otherwise it will override all queried results.
You need to set a path to that object and update the field you would like. In this example...
firebase.database().ref(1710172911409/L5F4BEuePS8qxUqDNoF/questionId).update("NEWQUESTIONID);
If you don't know the unique id of the pushed object, you can query for it. Learn more here
Using firebaseArray, is it possible to order by a specified child key:
(https://www.firebase.com/docs/web/guide/retrieving-data.html#section-queries)?
var fireRef = new Firebase(https://dinosaur-facts.firebaseio.com/dinosaurs);
var query = fireRef.orderByChild("echo").limitToFirst(25);
var todos = $firebaseArray(query);
However, by default, Firebase retrieves the keys in ascending order.
Children with a numeric value come next, sorted in ascending order.
Is there any way to get results in the descending order directly from firebaseArray? For example, do they have anything like .reverse() or .orderBy(desc, ...)?
This doesn't directly answer the original question, but for anyone using FirebaseUI Java/Android, with a RecyclerView, it's possible to order the RecyclerView in reverse, rather than using a query on Firebase:
Example:
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager)
(Ensuring you set both before adding the layout manager to the RecyclerView also)
Github issue: https://github.com/firebase/FirebaseUI-Android/issues/90#issuecomment-230643303
Here is how you can order by date. Just use queryOrderedByChild(dateKey)
and it will be in asc order, to make desc you can do array.reverse()
POST_REF.queryOrderedByChild(PostKeys.kPostDate).observeEventType(.Value, withBlock: {
snapshot in
var localPosts = [Post]()
for item in snapshot.children {
let postItem = Post(snapshot: item as! FDataSnapshot)
localPosts.append(postItem)
}
//Display at desc order
let reverseResults = localPosts.reverse() as [Post]
completion(postsArray: reverseResults)
}, withCancelBlock: { error in
completion(postsArray: nil)
})
I have a collection of data in Firebase that uses my own custom keys to match what we are expecting from our API when it is completed. However, I need to get the highest value from a key that we created.
Data:
-Results
+20150514-1
+20150514-2
-20150521-1
Field1: data
Field2: data
...
+20150521-2
+20150528-1
+20150528-2
+20150528-3
+20150528-4
+20150604-1
+20150604-2
+20150604-3
In this case the key fields under my Results are dates with instances, then full records under each instance (Field1, Field2, ...). I am getting the data using
var FireBaseData = new Firebase(FIREBASE.URL);
var Results = FireBaseData.child('Results');
var LastResult = Results.orderByPriority().limitToLast(1);
var LastDate = LastResult.$id;
return LastDate.substr(0, 8);
However, I am now trying to get access to the $id, or even a data field in that last record, but I cannot get data.
I have also tried:
factory('Result' ...
var FireBaseData = new Firebase(FIREBASE.URL);
var Results = FireBaseData.child('Results');
var LastResult = Results.orderByPriority().limitToLast(1);
return $firebaseObject(LastDate);
and then try to access the Field on the returned item, without success.
var myDate = Result.Field2;