My data looks like below,
"USERS": { "JACK": { "FirstName":"Jack", "LastName":"Wil" }, "JAY": { "FirstName":"Jay", "LastName":"Martin" } }
I'm writing a HTTPS triggered cloud function that first sends Node names (JACK, JAY) and then based on selection, sends details.
For sending the Node names, I'm just wondering if there is any way/function to get all the Node names under a Child without iterating through the snapshot and collecting the names using '.key'
I have gone through the below posts, but they discuss the iteration logic. So posting a new question.
Getting node names from Firebase database based on emailId match
Getting node names from Firebase database based on emailId match
Related
I'm trying to store locations nearby me for a test. I ran the following code:
var macro_query = db.collection("cafes");
macro_query = macro_query.where("name", "==", place.name); // check whether name exists
console.log("Checking final query: ", macro_query); // check whether it exists
macro_query.get()
.then(function(querySnapshot) {
console.log(querySnapshot.empty); // returns true if empty, returns false if place.name is already in database
if (querySnapshot.empty) {
db.collection("cafes").add({ // store it since it doesnt exist
name: place.name,
});
console.log("Added into database: ", place.name);
} else {
console.log("Location already in database");
}
});
The logic of the code is basically to check the database if place.name already exist. If it does, the script does nothing, otherwise the place should be added to the database.
The code works fine after a couple of tests, to make sure that it never adds the same location twice. But after running it many many times, i noticed that in my firestore there could be more than 2 unique keys with the same location (meaning, 3 locations with the exact same name)
Is my code failing somewhere and I'm not matching it correctly?
Side note, I'm wondering if this is because of my security rules?
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
This has nothing to do with security rules.
Your code has a race condition. Since there is some brief period of time between the query and the update, that means you could end up with multiple concurrent queries each adding a store with the same name. Unfortunately, for Firestore web and mobile clients, there is no way to make this sort of query atomic in order to avoid the race condition. Firestore transactions won't help you here, since you can't do a query inside of a transaction.
If you want to atomically check-and-set, the thing to check must the document ID, not a field in the document. If you're looking for a specific document by ID, you can then use a transaction to only create the document if it doesn't already exist. This also implies either one of two things:
Your store names are also valid document IDs
Or you have a dedicated collection for storing encoded store names as document IDs, with parallel documents in another collection that actually contain the store data.
If you choose #2, you are in for quite a bit of work to make that happen smoothly.
I have a real-time database on firebase which consists of ListFields. Among these fields, one field, participants is a list of strings and two usernames. I want to make a query to firebase database such that it will return the documents in which a particular username is present in the participants list.
The structure of my document is as follows :
I want to make a query such that Firebase returns all the documents in which the participants list consists aniruddh. I am using Flutter with the flutterfire plugins.
Your current data structure makes it easy to find the participants for a conversation. It does however not make it easy to find the conversations for a user.
One alternative data structure that makes this easier is to store the participants in this format:
imgUrls: {},
participants: {
"aniruddh": true,
"trubluvin": true
}
Now you can technically query for the the conversations of a user with something like:
db.child("conversations").orderByChild("participants/aniruddh").equalTo(true)
But this won't scale very well, as you'll need to define an index for each user.
The proper solution is to add a second data structure, known as an inverted index, that allows the look up of conversations for a user. In your case that could look like this:
userConversations: {
"aniruddh": {
"-LxzV5LzP9TH7L6BvV7": true
},
"trubluvin": {
"-LxzV5LzP9TH7L6BvV7": true
}
}
Now you can look up the conversations that a user is part of with a simple read operation. You could expand this data structure to contain more information on each conversation, such as the information you want to display in your list view.
Also see my answer heres:
Firebase query if child of child contains a value (for more explanation on why the queries won't work in your current structure, and why they won't scale in the first structure in my answer).
Best way to manage Chat channels in Firebase (for an alternative way of naming the chat rooms).
Trying to get the data from specific document from firestore.
database have First collection named "learner" and in each document of this another collection "request".
(each document is created with the same name as email)
so each document have emails as their name.
when im using a static address i get the correct response .
but of course the addressing i need to get is with the email which user will input,
Firestore.instance.collection("/learner/Qwr4#g.com/requests/").snapshots().listen(Elements);
Elements is the function which converts the response into a list
using this function i get correct response
but what i need is
this is what is need.
this function is giving me list of 0 length
void function(String email) { Firestore.instance.collection("learner").document(email).collection("requests").snapshots().listen(Elements); }
I'm having trouble setting up a search query within my Firebase database. I put the rules all the way of my search, but I always get the index error.
Below is my structure:
My Firebase database structure:
My query:
https://zoome-production-users.firebaseio.com/country.json?orderBy="fullname"&equalTo="Vitor Darela"
Erro:
{
"error": "Index not defined, add \".indexOn\": \"fullname\", for path \"/country\", to the rules"
}
In your REST call, you run a query on /country in your database. This query inspects each direct child node of /country to see if it matches your filter. In your JSON data /country/AD does not have a property fullname, so the query will not return anything. Even if you were to add the index that the error message tells you about, it will not return anything.
The query you are trying to do is not possible with your current data structure. You will need to modify your data structure to allow it. It seems that you're trying to to find the countries that have a user with a certain name. To allow that query, add an additional data structure that holds precisely that:
"countries_by_fullname": {
"Vitor Darela": {
"AD": true
}
}
With this additional structure you can find the list of countries by simply reading /countries_by_fullname/Vitor Darela.
Also see:
Firebase Query Double Nested
Firebase query if child of child contains a value
Currently I can't seem to find a way to delete an object from firebase list via REST api. For example I am trying to remove this from the list:
https://mrdapper.firebaseio.com/v0/users/41/favs.json?orderBy=%22id%22&equalTo=107657061
Posting a DELETE request doesn't work with query parameter.
You can't delete with a query, (although that would be awesome). But you can use the results to send a DELETE request.
Do a GET:
GET https://mrdapper.firebaseio.com/v0/users/41/favs.json?orderBy=%22id%22&equalTo=107657061
This will return the object and you can send a DELETE request for each item it returns.
DELETE https://mrdapper.firebaseio.com/v0/users/41/favs/<returned-id>.json
Now, you may not like sending one delete request per object. But, with your data structure this is necessary.
If you'd like to easily query and delete items, you could try this data structure:
/users/$user_id
/userFavs/$user_id/$fav_id
Store the favs in it's own location under the root. This will allow you to retrieve user data without always getting the favs.
For userFavs if you key off userid and the favid you can easily query and delete.
{
"userFavs": {
"41": {
"107657061": {
"note_count": 43633
}
}
}
}
Now you can easily get all of the the user's favorites by specifying the user's id. If you need to delete by an id, that is also a key. So you can now DELETE without querying.
DELETE https://mrdapper.firebaseio.com/v0/userFavs/41/107657061.json