Last night I was trying to index a nested object in firebase's database. Below is my object data and rules. Currently on orderedBy locID it is returning index not defined. Would appreciate any help thanks!
My Data:
My Rules:
{
"rules": {
"locations": {
"$uid": {
".indexOn": ["locID"]
}
},
".read": true,
".write": true
}
}
The code that triggers the error:
locData = db.child("Locations").order_by_child("locID").equal_to(someID).get()
The above is returning index not defined, using the pyrebase wrapper.
You're skipping a level in your query.
Firebase will query the immediate children of the location on which you execute the query. So in your case, it will query items 10S and 12S and try to order/filter on locID. Neither of these nodes has a child locID, since that property is one level deeper in the tree.
Pyrebase likely talks against the Firebase REST API, which will fail when there is no index for the field you're trying to order/filter on.
It's a bit hard to be certain for your use-case, but in general the solution to this problem is to keep a reverse index. In this case that could be a list that keeps track of the 10S/12S thing for each locID.
locByLocID: {
5689: "10S",
8223: "12S"
}
Related
I m using firebase Database .Planning to have quite a large data collection , I'm trying to use indexes . Making some tests, i don t know if rules i implement are correct let alone improve the queries .Is there a way to have a feedback on those indexations (correct or not, improve or not)
right now i have one class as described in the picture and i have the created the following rule
{
"rules": {
".read": true,
".write": true,
"users": {
"$user_id": {
".indexOn": ["user_id", "username"]
}
},
}
Since i get no feedback from firebase, i don t know if this is correct and improves anything .
Firebase's server-side rules are meaningless without seeing the code that exercises them.
But at a first glance it seems you're defining your indexes too low in the tree: indexes need to be defined at the location where you run the query. So if you want to run a query on /users, you need to define the indexes on /users:
{
"rules": {
".read": true,
".write": true,
"users": {
".indexOn": ["user_id", "username"]
}
}
}
This is a rough example of what my database looks like.
"userA": {
"uf": {
"userB": "0"
}
},
"users": {
"userA": "0",
"userB": "0",
"userC": "0"
}
And this is a rough example of the rule I am trying to write.
//USER ID
"$uid": {
//USER FRIENDS
"uf": {
//FRIEND USER ID
"$fuid": {
".write": "$uid === auth.uid &&
root.child('users').hasChild($fuid)",
}
},
},
And this is what I am trying to get working in the simulator
//Location /userA/uf/
//Data { "userC": "0" }
It seems that the security rule will always deny a write when the "key" for a data key value pair is a variable in my rules, in this case "$fuid". The simulator will return the messages "Simulated set denied" and "Write denied" but won't give me any additional details. I could get around this by writing the following.
//Simulation Method set
//Location /userA/uf/userC/
//Data { "0": "0" }
But this feels like it's writing unnecessary data to my database. What is the best practice here? Thanks.
Your rules give access to {uid}/uf/{fuid} but you're trying to write at {uid}/uf.
That {"0": "0"} is indeed unnecessary, you can just write "0".
If you want to write multiple friends at once, you can perform a multipath update, or modify your rules to allow writing directly at {uid}/uf and ".validate" the children.
Side note: if your users can be deleted, if user A has user B as a friend and user B is deleted, your rules won't allow user A to remove user B from the friends list. You should take care of that by changing the rules to allow the deletion of friends that do not exist, or by setting up an onDelete() triggered cloud function that would do the cleanup.
I'm using a structure for saving the users friends in the realtime database like this:
friends
$user_id:
-$friend_id1
-name: ""
-id: ""
-$friend_id2
(...)
My rules look like this:
"friends":{
"$id": {
".read": "auth != null && $id === auth.uid",
".write": false,
},
},
Within my iOS App I can access the friends of others although I should not be able to do so.
Also this seems to mainly be an issue in one single view, since when I'm changing the
"read": false
I can still read this data in that specific view.
In all other views however I can not do that, although the code is nearly the same (except for observing (.childAdded) instead of (.value).
I´m calling
let friends_ref = Database.database().reference().child("friends").child(self.friend_id)
friends_ref.observeSingleEvent(of: .value, with: { (snapshot) in
//CODE
})
Also, the time it takes to load the friends, so to get the callback, is very long taking even about >5 Seconds to get 5 friends.
How can this happen?
Best regards
I've been using the Microsoft Graph to perform Delta Queries on Users. My requirement is to return the values of ExtensionAttributes (custom properties synced to Azure AD from AD on-prem).
The following request works:
/v1.0/users?$select=extension_8928938292839829832_value
And returns the extension attributes as desired:
"value": [
{
"extension_8928938292839829832_value": "String1",
}
However when trying to use the Delta Query and including extension attributes:
/v1.0/users/delta?$select=extension_8928938292839829832_value
The value returned is just the basic user properties, NOT the requested extension value:
"value": [
{
"deletedDateTime": null,
"accountEnabled": true,
"assignedLicenses": [],
etc, etc, etc
},
I'm not sure if the extension values are exposed in the v1.0 Delta Query.
Anyone have thoughts on how to approach this issue? Grabbing these extension attributes and using the delta query to track changes is vital to my solution.
I'm afraid this isn't supported at the moment. This is listed in the Known Issues:
Delta query
OData context is sometimes returned incorrectly when tracking changes to relationships.
Schema extensions (legacy) are not returned with $select statement, but are returned without $select.
Clients cannot track changes to open extensions or registered schema extensions.
I have an iOS app where I have to implement a simple chat between two users. I am registering the users to Firebase during app registration. All work good. But when I have to start a chat thread I am searching for a user using search indexes but I am getting following error:
Using an unspecified index. Consider adding ".indexOn": "phone" at /use_frameworks_beta_2/searchIndex to your security rules for better performance
Using an unspecified index. Consider adding ".indexOn": "email" at /use_frameworks_beta_2/searchIndex to your security rules for better performance
Using an unspecified index. Consider adding ".indexOn": "name" at /use_frameworks_beta_2/searchIndex to your security rules for better performance
The Rules in Firebase console is as follows
{
"rules": {
".read": true,
".write": true,
"searchIndex": {
".indexOn": ["email","phone","name"]
}
}
}
Please help me fixing this error.
The .indexOn specifier must be placed in the rules at the location containing the indexed keys. Update your rules to place .indexOn at the correct location in the tree:
{
"rules": {
".write": true,
".read": true,
"use_frameworks_beta_2": {
"searchIndex ": {
".indexOn": ["email","phone","name"]
}
}
}
}