How can i modify the data sent by an RTK query? - redux

I'm getting some data back from an RTK query, everything works find, except I need to delete some data from that array
const [addSuggest, { isLoading:isLoadingSuggest, data:dataSuggest }] = useAddSuggestMutation();
From this i'm showing a set of suggestions, that can be added to another dataset by the user. When it's added, I need to remove it from the dataSuggest so it's not shown anymore.
Any idea how i can do that ?
Thanks

Related

Firebase realtime database returns only one entry

I am currently trying the firebase realtime database and I am executing my first queries. I am using the Firebase Admin console for querying.
I now have a problem that I only get one entry back by a query when using ordering. My data structure looks like the following:
The query I am using is:
firebase().database().ref('/Office1/Department1/HR').orderByChild('Rank').on('value', function (snapshot) {
snapshot.forEach(function(childSnap){
console.log(childSnap.val());
});
})
As an output I only get:
{"Name":"Charles","Rank":1}
As you can see the ordering works, however it only shows one item instead of all 3 entries.
Somebody knows how to fix this issue?
Thanks

How to query one field then order by another one in Firebase cloud Firestore?

I'm struggling to make a (not so) complex query in firebase cloud firestore.
I simply need to get all docs where the id field == a given id.
Then order the results by their date and limit to 10 results.
So this is my current situation
db.firestore().collection('comments')
.where("postId",'==',idOfThePost)
.orderBy('date','asc').limit(10).get().then( snapshot => {
//Nothing happens and the request wasn't even been executed
})
I can get the result only if i don't use the orderBy query but i have to process this sorting for the needs of my application.
Someone has an idea to help me to fix this ?
thanks
You can do this by creating an Index in the firestore.
The first field of the index should be the equality field and the second field of the index should be the order by field.
Given your problem, you would need the following index:
first field: postId, 'asc'
second field: date, 'asc'
Please check the doc. It says
However, if you have a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field
you can try this code
db.firestore().collection('comments')
.where("postId",'==',idOfThePost)
.orderBy('postId')
.orderBy('date','asc').limit(10).get().then( snapshot => {
.....
})
My Workaround
If you're googling this you've probably realized it can't be done traditionally. Depending on your problem though there may be some viable workarounds, I just finished creating this one.
Scenario
We have an app that has posts that appear in a feed (kind of like Reddit), each post has an algorithmic score 'score' and we needed a way to get the 'top posts' from 12-24 hours ago. Trying to query sorted by 'score' where timestamp uses > and < to build the 12-24 hour ago range fails since Firebase doesn't allow multiple conditional querying or single conditional querying with an descending sort on another field.
Solution
What we ended up doing is using a second field that was an array since you can compound queries for array-contains and descending. At the time a post was made we knew the current hour, suppose it was hour 10000 since the server epoch (i.e. floor(serverTime/60.0/60.0)). We would create an array called arrayOfHoursWhenPostIsTwelveToTwentyFourHoursOld and in that array we would populate the following values:
int hourOffset = 12;
while (hourOffset <= 24) {
[arrayOfHoursWhenPostIsTwelveToTwentyFourHoursOld addObject:#(currentHour+hourOffset)];
hourOffset++;
}
Then, when making the post we would store that array under the field hoursWhenPostIsTwelveToTwentyFourHoursOld
THEN, if it had been, say, 13 hours since the post was made (the post was made at hour 10000) then the current hour would be 10013, so we could use the array-contains query to see if our array contained the value 10013 while also sorting by algorithm score at the same time
Like so:
FIRFirestore *firestore = [Server sharedFirestore];
FIRCollectionReference *collection = [firestore collectionWithPath:#"Posts"];
FIRQuery *query = [collection queryOrderedByField:#"postsAlgorithmScore" descending:YES];
query = [query queryWhereField:#"hoursWhenPostIsTwelveToTwentyFourHoursOld" arrayContains:#(currentHour)];
query = [query queryLimitedTo:numberToLoad];
Almost Done
The above code will not run properly at first since it is using a compound index query, so we had to create a compound index query in firebase, the easiest way to do this is just run the query then look at the error in the logs and firebase SDK will generate a link for you that you can navigate to and it will auto-generate the compound index for your database for you, otherwise you can navigate to firebase>database>index>compound>new and build it yourself using hoursWhenTwelveToTwentyFourHoursOld: Arrays, score: Descending
Enjoy!
same here, it is weird why can't. below is another sample. can't get the result. Hoping firebase can reply about this and update the document.
dbFireStore.collection('room').where('user_id.'+global.obj_user.user_id,'==',true).orderBy('last_update').get().then((qs)=>{
console.log(qs);
});
using other work-around solution is javascript array and array.sort()
I ran into the same issue yesterday on Android. The Callback was just not called. Today I suddenly got an error message. FAILED_PRECONDITION: The query requires an index. It even contains a URL in the error message to generate that index with one click.
It seems that if you want to use orderBy on your data, you need to create an index for that field. The index also needs to be in the correct order (DESC, ASC).
As per firestore document,
If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.
So just click that link you get in Logcat, it will be redirected to create index page, just create index. It will take some time. after enabling composite index, you will get the result as your requested query.
Stumbled across this looking for help when i found that using the orderBy function didnt work and the documentation still says it does not support it. A bit weird and unclear to be honest, because it does support it so long as you index your Firestore database. For example, this query now works fine for me having set up indexing:
const q = query(docRef, where("category", "==", 'Main'), orderBy('title', 'asc')
Indexing in Firestore
Console Log that even gives you the url to automatically create the index if you try and run with the above command.
Maybe I am missing something, or a later version of Firebase (I am using v9) simply does support it.

Cloud Functions for Firebase: How to get the value of the data that was deleted

When I add a cloud function responding to a delete event, like this one:
exports.onDeleteSector = functions.database.ref('/sectores/{idSector}').onDelete((event) =>
I can get the key to the sector being deleted in event.params.idSector proving the trigger works, however, event.data.val() returns null.
The deleted record contains the references to the children to be deleted. How can I get those before the parent is gone?
Thanks
event.data.val() returns null, because that's the current value of the database at the time of the trigger. For all kinds of database triggers, this is going to be the case. For onDelete, this will always be null.
If you want to see what was previously at that location, before the event happened, take a look at event.data.previous.val(). Also see the docs for DeltaSnapshot, which is the data type for event.data.
The value of the entry being deleted is available at:
event.data.previous.val()
Current way of getting the deleted data from onDelete trigger is like this:
exports.userDeleted = functions.firestore
.document("users/{userId}")
.onDelete((snapshot) => {
const original = snapshot.data();
// original contains the deleted data
});

I tried deleting records by id, but it happens, I have found the last record is deleted

I tried deleting records by id, but it happens, I have found the last record is deleted,
//Index method just
Report::all( ) ;
//Delete mesthod
Report::delete($id);
sorry if very quick question, I use a mobile phone now
Thank You
According to the docs, one should find the specific record with a query and perform delete on that.
https://laravel.com/docs/5.3/eloquent#deleting-models
Based on their example your code should be more like:
$record = Record::where('id', $id)->delete();
If you have observers which fire on Record delete then you should use Model's delete() method as:
Record::find($id)->delete()
Or you can just use query builder's delete method as:
Record::where('id', $id)->delete()

Meteor pagination: cursor fetch limit with getmore

I have an infinite scroll page where I'm not using Meteor templates to draw the items. The reason for that belongs in a whole other thread. I'm trying to figure out how to paginate the data without fetching all the items at once. I have an idea about using a limit on the cursor, but can't find any real samples online of the proper way to do this.
Should the server call return the cursor itself or just the find with limited data set? If the server doesn't return the cursor itself, won't I lose position when I try to fetch the next set of results?
Also, I want to make sure to retrieve data from the same cursor. Like if there are currently 100 items and I fetch 20, I expect the next 4 fetches to get 20-40, 40-60, 60-80, and 80-100. If in the interim some items got inserted or deleted, I don't want it to mess up the fetches. I am handling reactivity separately and letting users decide when to update the items (which should reset the cursor).
Help/advice appreciated!
What you would usually do is this:
var cursor = collection.find({},{limit:100+20*page});
The first {} is obviously the selector!
Docs:
http://docs.meteor.com/#/basic/Mongo-Collection-find
You don't have to worry about returning only the values 100-120 and then 120-140 etc. since meteors ddp does that for you!
If you were using meteor's blas or you just want to have the reactivity, you should probably store the page variable in the Session or create a dependancy:
https://manual.meteor.com/#deps-asimpleexample

Resources