In the given code below I am trying to allow only those post documents from the "Posts" collection to be read where the distance between the requesting user and the post author is less than or equal to the user specified radius. Each user document in "Users" collection has a Geopoint field and a Radius field and each author field in the every post document of "Posts" collection corresponds to the author's uid. Both the requesting user's and the Author's uids are to be retrieved from the "Users" collection using the get() function. However, I am getting an error while using the get() function to retrieve User's and the author's data such as Geopoint and radius.
Error: simulator.rules line [44], column [14]. Function not found error: Name: [get].; Error: Invalid argument provided to call. Function: [get], Argument: ["||invalid_argument||"]
// Posts collection
match /Posts/{postId} {
allow read: if isUserCloseToAuthor(request.auth.uid, resource.data.Author);
}
}
function isUserCloseToAuthor(userId, authorUid) {
let user = get(/databases/$(database)/documents/Users/$(userId)).data;
let userRadius = user.Radius;
let author = get(/databases/$(database)/documents/Users/$(authorUid)).data;
let authorGeoPoint = author.Geopoint;
let userGeoPoint = user.Geopoint;
let distance = userGeoPoint.distance(authorGeoPoint);
return distance <= userRadius;
}
I have attached an image of the simulator. Also, Note that the code given is just the part causing error and not the whole thing.. The uid I used in the simulator is a real uid from the database.
I tried to do the whole thing in a single line instead of using a function and still got the same error while using get() function
allow read: if get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.Geopoint.distance(get(/databases/$(database)/documents/Users/$(resource.data.Author)).data.Geopoint)<=get(/databases/$(database)/documents/Users/$(request.auth.uid)).data.Radius
I tried passing my database name in the $(database) variable into the get() function and still it won't work. I tried playing around with the paths specified in the get() function and still get the same error. I tried accessing this using my client but the request still gets denied. A little help with this would be highly appreciated. Thank You