How to add conditionally data to Firebase Firestore? - firebase

I'm working on a project which is about to go live, using Google Firebase Firestore for data.
The users will be adding review through a form, but currently that form submit is:
firebase
.firestore()
.collection(props.collection)
.add({
item: entryName,
value: parseInt(rate),
description
})
But honestly I wouldn't really love to let users adding items without my confirmation.
Any idea how it could be done? If not only Firebase Firestore it's fine as well, but I have no ideas.

Related

Flutter: How to send notifications to users when a document with specific field values is created. (how to subscribe to topic with condition)

I want to add a "Notify me" function in my flutter app where the user enters a date and other values so when a document is created with those values, the user can get a notification. I'm unable to add any conditions in fcm.subscribeToTopic();.TIA.
You can create a cloud function and deploy it to firebase Functions, and in that function specify the path of collection (in which your document is creating) and it will trigger when a new document is created (write function for that. example ). in order to send notifications to that user store that user's device Token. you can find cloud Firestore Functions here.
some articles that help: firebase tiggers
Subscriptions to a topic are unconditional. The simplest way I can think of to implement this use-case is to create topics that contain the specific conditions that you are interested in, like date_20210823_value_yourvalue.

How can I assign content to specific signed in user within Flamelink and React Native?

I am using Firebase with React Native and just started with Flamelink as my CMS.
Before using Flamelink, I was able to obtain specific content from a logged-in user via firebase realtime database because the schema was:
**fruits**
mwaKRypUrNb8rJdsj9YUxw0sA892
-M055RlZ7HBzgRD9LS1d
-M055Why2psB2kqE4mYO
-M05W3LK8C1kgFsPNRqa
-M05W47cbhJ2VA_Y2nnT
-M05W5Wu9Pp6TZJJPc8t
-M05W6Xc_iP1upbyENpj
-M05W7JnVt3t3DsjNaRT
-M05W8OuZph-1gDDY9UX
-M05W9Fws0_hsNCAyKDt
**users**
-zFWGnGhEmbfhjAJsUT99ZbkJBZ33
As you can see, the fruits schema had user-specific content.
With Flamelink the schema looks like this:
**flamelink**
--environments
----production
------content
--------**fruitEntryForm**
----------en-US
------------1582070066847
------------1582074440836
------------1582080914427
**schemas**
**media**
**permissions**
**settings**
**users**
--zFWGnGhEmbfhjAJsUT99ZbkJBZ33
My react native code is below:
const user = navigation.getParam("user");
const currentUserData = await firebase
.database()
.ref("flamelink/users")
.child(user.uid)
.once("value");
const fruits = await app.content.get("content");
How can I modify Flamelink so that I can manage content based on which user is signed in?
From my understanding, your Fruits collection can be linked to multiple users and the main problem is that the Firebase Realtime Database does not allow querying for a value inside an array. Cloud Firestore is a bit better in that regard.
What I would suggest you do to simplify your queries is to turn the relationship on its head. I would have the Fruits collection that you currently have without a relationship to any users. Then I would create a "Users Fruit" collection that has a field for the user's UID with a relational select field that maps to multiple entries in your Fruits collection.
From your React Native app, you can then use our SDK to query the "Users Fruit" collection and expand the related Fruits.
Depending on the version of the SDK you are using, the syntax might differ a bit, but for the latest SDK it will look like this:
const userFruit = await app.content.get({
schemaKey: 'usersFruit', // assuming schema key is this
orderByChild: "user", // assuming your field is called 'user'
equalTo: "mwaKRypUrNb8rJdsj9YUxw0sA892", // assuming this is your user's uid
populate: ['fruits'] // assuming you called the select relational field 'fruits'
})

Use Firestore autoID to retrieve and display data in Flutter

im new to flutter and dont know exactly what to search for this one but, can we use the auto generated ID like in the picture to retrieve all that data UNDER that ID? if so, how ? In a similar question that I stumbled upon, they use database.reference() but its a Realtime Database and not FireStore
Im using Firebase Cloud Firestore
There is no AutoId in firebase but here is a quick way to set as auto id
Forexample ;
1- create yourModel ( which one u gonna send as model to firebase )
2- DatabaseReference firebaseDatabase;
3- firebaseDatabase =FirebaseDatabase.instance.reference();
4- firebaseDatabase.child("table_name").push().set( yourModel.toJson() );
Also for getting data u can write code like that
var result= firebaseDatabase.child("table_name").once().then(
(DataSnapshot datasnapshot){
Map<dynamic,dynamic> values= datasnapshot.value;
values.forEach((key,value){
print("key:"+key+" value:"+value["name"]);
});
}
);
print(result);
I tried it and works great
Have a nice day !!!
I'm guessing you're asking about subcollections.
If you read a document (by its (auto-generated or not) key), you get back that document. You don't get back data from any subcollection. That will require a separate read operation for each subcollection under the document that you want to read.

Fetch documents from multiple subcollections - Firebase [duplicate]

This question already has answers here:
Firestore query subcollections
(11 answers)
Closed 3 years ago.
I'm creating a web app for admin with firebase integration, this web app is used by admin to monitor posts or comments posted by user from Android or iOS app developed in ionic.
I'm using Firebase firestore, and following is the database design
posts/{postsDocument}/comments/{commentsDocument}.
The comments here is sub-collection for postsDocument that holds all the comments for a particular post. Also postDocument and commentsDocument contains Firebase uid of the user.
My problem is, whether it's possible to fetch all the comments commented by a particular user. I can query a single collection, but in this scenario commentsDocument is a subcollection and i want to fetch all comments across all the post by a user for monitoring purpose.
You can check firebase collection group query
var commentsQuery = db.collectionGroup('comments').where('commentOwnerID', '==', userId);//userId - firebase id of the user who commented
commentsQuery.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id, ' => ', doc.data());
});
});
Also you might have to create index that supports your query, check console for the the link for creating index

Firestore Search - LIKE

I want to search the "displayName" in documents nested within a collection, or more specifically the data is as follows:
users -> $idstring -> displayName
Ive come up with the following using AngularFire but its still not quite working for me.. I need to check displayName against the first 3 chars of val (user entered) and bring back results that start with that, I need a kind of LIKE search operation to occur, is this possible with firestore.. so far its just returning almost everything in my users collection
this.itemsCollection = this.aft.collection<iUser>('users', ref => ref.orderBy("displayName").startAt(val))
As mentioned on the Cloud Firestore Documentation:
Cloud Firestore doesn't support native indexing or search for text
fields in documents. Additionally, downloading an entire collection to
search for fields client-side isn't practical.
And to enable full text search of your data, you'd need to use a third-party search service like Algolia or ElasticSearch.
The documentation actually provides a guide on how to integrate Algolia with Firebase.

Resources