how to Retrieve firebase database start from bottom to up? - javascript

I have a set of items in firebase realtime,
when i Retrieve data the old data appears at the first.
Already i tried
Firebase.database().ref().child('chains').orderByKey().startAt();
but the data didn't appear
I want upload new item and it appear at the first how do i do this?
this my code:
this my database

How do you determine which items is newer? Is there any child node containing timestamp? If yes then you should use orderByChild():
firebase.database().ref('chains').orderByChild('addedAt')
However this will sort by ascending order and hence oldest items will be retrieved first. If you are looking for latest 50 items then you can use limitToLast method and then reverse the order using Javascript:
firebase.database().ref('chains').orderByChild('addedAt').limitToLast(50)
If you are ordering them using the item keys then you can use orderByKey but to get items in descending order i.e. newest first, you'll have to use same logic with limitToLast.

Related

How to load data onScroll in ReactNative with Firebase Realtime Database

i want to load data from my realtime database, but only 15 entries everytime, because the database is huge. The database has a name of the vocable and got information about it like translations and stats. I want to sort it alphabetic by the value "wordENG", but there is a problem, when i use orderByChild like this:
database()
.ref(`vocables/${UID}`)
.orderByChild("wordENG")
.startAt(requestCount)
.limitToFirst(15)
.once("value")
.then(snap => {
console.log(snap.val());
})
When i try to use startAt, to get the data on scrolling, i get the problem that startAt need to be a string, so a word of the database list. I don't want to store this word everytime and search for new one after that, but currently i cannot see another way. Is there a way to get data alphabetic on scrolling with a number to count or do i need to realize it with saving the last word and search from there?
Pagination with Firebase queries work based on knowing the anchor item, not on offsets. So you will need to indeed know the wordENG value of the node to start at (or start after with the relatively new startAfter method), and possibly the key (if there may be multiple child nodes with the same wordENG value.
If you're new to Firebase, I recommend also reading some of the previous questions about pagination, as this comes up regularly.

Firebase query requires an index - Boolean and Date

Currently i have a structure like this:
plannedDate:Timestamp,
deleted:Boolean
And i need to perform a query such as:
.where('plannedDate','>=',someDate)
.where('plannedDate','<=',someOtherDate)
.where('deleted','==',false)
As it should, based on firestore behaviour, it is asking me an Index to perform the query.
Have trying:
plannedDate Ascending delete Descending
and
plannedDate Ascending delete Ascending
So far it keeps asking me an index.
Detail: The link to create a new index, given by firebase exception, does not open. Probably firestore exception is still referencing an old way to do it. (js error is shown at console when opening the link)
So, question is, how to create an index to a query such as that?
Edit: Adding the link generated by the error
firebase generated link

Using PFQuery to retrieve PFInstallation objects containing a specific item in their array?

I'm trying to send push notifications to users if one of their favorite foods appear on a dining menu on a given day. For example, if a user lists pizza, spaghetti, and potato as his favorite foods, and on a particular day, the menu contains pizza, the user would receive a push notification.
This is how I'm currently storing the favorite foods
PFInstallation *installation = [PFInstallation currentInstallation];
[installation addUniqueObject:foodName forKey:#"favorites"];
[installation saveInBackground];
In the available functions, I found these, but after closer inspection, they don't seem to be what I need
- (void)whereKey:(NSString *)key containedIn:(NSArray *)array;
- (void)whereKeyExists:(NSString *)key;
(I'll be using cloud code to retrieve the data, but I'm assuming there is an analogous javascript method.)
Is there a way to get a query of all the installation objects containing a given item in their favorites array?
Also, although I map each user to an array of foods, it seems more intuitive to map each food to an array of corresponding users. However, I don't want users to enter login info, so I'm trying to avoid the PFUser class, and I don't know if there is another way of adding a user to an array. I'm wondering if this is possible, and if it would make it easier to retrieve the information.
As per the documentation on Queries on Array Values, if you want to find rows where a value exists in an array:
[query whereKey:#"favorites" equalTo:#"pizza"];
This will return all rows where "pizza" is in the array column "favorites".
you can go for PFRelation Class on Parse.com for relational queries.
and for query you can go for blocks (sorry it's in swift coding)
let query : PFQuery = PFQuery(className: "GameScore")
query.whereKey("favorite", containedIn: ["item1", "item2"])
query.findObjectsInBackgroundWithBlock({(objects: AnyObject[]!, error: NSError!) in
// your code
})

How to save HTML list number in JSON

I have a HTML page where I can create lists from a javascript. It saves the data in JSON. To the list there is also a javascript, which makes it possible to drag and sort the list items if you want to do so. All the javascripts are independent of each other.
My problem is that I don't know how I should index these list items in the order they have been placed, and afterwards send it to the database. Of course all this should happen on the client before sending it to database.
Remember this question is ONLY about storing the right index number in each list item; in the right order. If I would change the order of a list item, the index number should change too, so it will be saved in the right order.
You can iterate over the collection just before saving them and assign the current values.
I did this with a menu I built once. In jQuery I did something like this:
$("#mylist li").each(function(inx){
$(this).attr('index',inx);
});
Then, when you save them, include the index attribute so you know the final order.
EDIT: per comment by #Boaz

Most Efficient way of Filtering an Html Table?

I have an ajax function which call a servlet to get list of products from various webservices, the number of products can go up to 100,000. I need to show this list in a html table.
I am trying to provide users an interface to filter this list based on several criteria. Currently I am using a simple jQuery plugin to achieve this, but I found it to hog memory and time.
The Javascript that I use basically uses regex to search and filter rows matching the filtering criteria.
I was thinking of an alternate solution wherein I filter the JSON array returned by my servlet and bind the html table to it. Is there a way to achieve this, if there is, then is it more efficient than the regex approach.
Going through up to 100,000 items and checking if they meet your criteria is going to take a while, especially if the criteria might be complex (must be CONDO with 2 OR 3 bedrooms NOT in zip code 12345 and FIREPLACE but not JACUZZI).
Perhaps your servlet could cache the data for the 100,000 items and it could do the filtering, based on criteria posted by the user's browser. It could return, say, "items 1-50 of 12,456 selected from 100,000" and let the user page forward to the next 50 or so, and even select how many items to get back (25, 50, all).
If they select "all" before narrowing down the number very far, then a halfway observant user will expect it to take a while to load.
In other words, don't even TRY to manage the 100,000 items in the browser, let the server do it.
User enters filter and hits
search.
Ajax call to database, database has indexes on appropriate
columns and the database does the filtering.
Database returns result
Show result in table. (Probably want it to be paged to
only show 100-1000 rows at a time
because 100,000 rows in a table can
really slow down your browser.
Edit: Since you don't have a database, the best you're going to be able to do is run the regex over the JSON dataset and add results that match to the table. You'll want to save the JSON dataset in a variable in case they change the search. (I'm assuming that right now you're adding everything to the table and then using the jquery table plugin to filter it)
I'm assuming that by filtering you mean only displaying a subset of the data; and not sorting.
As you are populating the data into the table add classes to each row for everything in that row you want to filter by. e.g.:
<tr class="filter1 filter2 filter3">....
<tr class="filter1 filter3">....
<tr class="filter2">....
<tr class="filter3">....
Then when you want to apply a filter you can do something like:
$('TR:not(.filter1)').hide();
I agree with Berry that 100000 rows in the browser is bit of a stretch, but if there's anything that comes close to handling something of that magnitude then it's jOrder. http://github.com/danstocker/jorder
Create a jOrder table based on your JSON, and add the most necessary indexes. I mean the ones that you must at all cost filter by.
E.g. you have a "Name" field with people's names.
var table = jOrder(json)
.index('name', ['Name'], { sorted: true, ordered: true });
Then, for instance, this is how you select the records where the Name field starts with "John":
var filtered = table.where([{ Name: 'John' }], { mode: jOrder.startof, renumber: true });
Later, if you need paging in your table, just feed the table builder a filtered.slice(...).
If you're getting back xml, you could just use jQuery selection
$('.class', context) where context is your xml response.
From this selection, you could just write the xml to the page and use CSS to style it. That's where I'd start at first, at least. I'm doing something similar in one of my applications, but my dataset is smaller.
I don't know what you mean by "bind"? You can parse JSON and then use for loop (or $.each()) to populate ether straight HTML or by using grid plugin's insert/add

Categories

Resources