I'm trying to make an app, where you have to choose a team name, and when the user enter his/hers team name, I want to check if the name already is in use.
teamNameTextField is a UITextField and for some reason no matter what I type in the text field, it prints "Team name is not in use". I don't know what I'm doing wrong here, can somebody help me?
My code:
rootRef.child("teams").queryOrderedByChild("teamName").queryEqualToValue(teamNameTextField.text).observeSingleEventOfType(.Value, withBlock: { (snap) in
if (snap.value is NSNull) {
print("Team name is not in use")
} else {
print("Team name is already in use")
}
})
my JSON data tree:
{
"teams" : {
"pbXvXYOKmJQqwSQZ9IlBykG7x1P2" : {
"teamName" : "Test111"
},
"owidUDkEnbCOsmNSoSFu2o2iu4y38RKJNF" : {
"teamName" : "Test222"
},
"pdnJCDmcdjsiHDFb8349HGD8372bfdhb" : {
"teamName" : "Test123"
}
}
}
My database rules:
{
"rules": {
".read": true,
".write": true
}
}
I realized the problem!
I tried to type print(teamNameTextField.text), and it printed: "Optional("Test111")"
So you have to put a ! after teamNameTextField.text, then it will only print "Test111"
Related
I'm trying to restrict user access to some high-level node but want to give some users ability to read some of the subnodes.
My data structure is like that:
{
"COMPANIES" : {
"COMPANY1" : {
"id" : "COMPANY1",
"name" : "COMPANY1 COMPANY"
},
"COMPANY2" : {
"id" : "COMPANY2",
"name" : "COMPANY2 COMPANY"
}
},
"USERS" : {
"xZgFtwVyQFsPK3428YCa3NekOEF3" : {
"company" : {
"COMPANY1" : true,
"COMPANY2" : true
},
"email" : "ss#sss.com",
"uid" : "xZgFtwVyQFsPK3428YCa3NekOEF3",
"username" : ""
}
}
}
and so on.
And my rules for this:
{
"rules": {
"USERS": {
"$uid": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid"
}
},
"COMPANIES": {
"COMPANY1": {
".read": "root.child('USERS').child(auth.id).child('company').child('COMPANY1').val() == true",
".write": false
}
}
}
}
The code I'm using to access this data looks like that (in Swift):
refFire.child("COMPANIES").child("COMPANY1").observe(DataEventType.value, with: { (dsnap) in
Also, I tried this one:
refFire.child("COMPANIES").queryOrdered(byChild: "COMPANY1").queryEqual(toValue: "COMPANY1").observe(DataEventType.value, with: { (dsnap) in
And this one (unfortunately, I don't really understand the idea behind queryEqual, so I've tried different approaches):
refFire.child("COMPANIES").child("COMPANY1").queryOrdered(byChild: "COMPANY1").queryEqual(toValue: "COMPANY1").observe(DataEventType.value, with: { (dsnap) in
So I want to give access to COMPANY1 to users with COMPANY1 = true in their profile (and deny everyone else). Rules seemed to be relatively straightforward but obviously I'm doing something wrong here because even users with COMPANY1 = true can't read company information, the permission is denied.
We are using learninglocker and I am trying to query its mongodb. learninglocker puts escaped urls as object names, which makes them more difficult to search. I am returning 0 results when I would expect to return several.
My find is as follows:
{"statement.object.definition.extensions.http://activitystrea&46;ms/schema/1&46;0/device.device_type": "app"}
I assume that this should be escaped somehow, however, am unsure how.
http://activitystrea&46;ms/schema/1&46;0/device
Sample object:
"statement": {
"version" : "1.0.0",
"actor" : { },
"verb" : { },
"context" : { },
"object" : {
"definition" : {
"extensions" : {
"http://activitystrea&46;ms/schema/1&46;0/device" : {
"device_type" : "app"
}
}
}
}
}
I am trying to allow users to start games with and follow other users by searching their username. I need to be able to make sure that a user with that username exists. I was using the following code but although the if is called the else does not get called when it should.
let checkWaitingRef = Firebase(url:"https://test.firebaseio.com/users")
checkWaitingRef.queryOrderedByChild("username").queryEqualToValue("\(username!)")
.observeEventType(.ChildAdded, withBlock: { snapshot in
if snapshot.value.valueForKey("username")! as! String == username! {
} else {
}
JSON data tree
{
"097ca4a4-563f-4867ghj0-6209288bd7f02" : {
"email" : "test1#tes1.com",
"uid" : "097ca4a4-563f-4867ghj0-6209288bd7f02",
"username" : "test1",
"waiting" : "0"
},
"55a8f979-ad0d-438u989u69-aa4a-45adb16175e7" : {
"email" : "test2#test2.com",
"uid" : "55a8f979-ad0d-438u989u69-aa4a-45adb16175e7",
"username" : "test2",
"waiting" : "0"
}
}
Easy fix:
Don't use .childAdded as the block will not execute when the query doesn't find anything.
Instead use .Value and check for NSNull
let checkWaitingRef = Firebase(url:"https://test.firebaseio.com/users")
checkWaitingRef.queryOrderedByChild("username").queryEqualToValue("\(username!)")
.observeEventType(.Value, withBlock: { snapshot in
if ( snapshot.value is NSNull ) {
print("not found)")
} else {
print(snapshot.value)
}
})
My app is using Firebase's Facebook authentication and I have the following users collection:
{
"fsdjf34hf98wjefj" : {
"uid" : "facebook:543634634536"
},
"3298djwhy9hwd34234" : {
"uid" : "facebook:7658899965432"
}
}
I'm trying to restrict access so that a user can view only his own user:
{
"rules": {
".write": true,
"users": {
".read": "auth.uid === data.child('uid').val()",
}
}
}
I get the following error when trying to access a user from JavaScript:
Error: permission_denied: Client doesn't have permission to access the
desired data.
Does anyone know the correct syntax for the .read section? I've tried many things but I don't seem to get the hang of the logic behind it.
Assuming your collection is:
"users": {
"fsdjf34hf98wjefj" : {
"uid" : "facebook:543634634536"
},
"3298djwhy9hwd34234" : {
"uid" : "facebook:7658899965432"
}
}
Your rules will need to be something like:
{
"rules": {
"users": {
"$user": {
".read": "auth.uid === data.child('uid').val()",
}
}
}
}
The $user in there applies to every user node.
Note that the easiest way to troubleshoot such issues is by using the Simulator tab in the Firebase dashboard to simulate the operation. It will tell you exactly why the operation is allowed/rejected, while the SDK will (intentionally) only tell you that the operation was rejected.
I'm trying to read firebase data and then print the data to the console, but nothing makes it to the console.
function readData(){
var listRef = new Firebase('https://----.firebaseIO.com/');
listRef.on('child_added', function(snapshot) {
var msgData = snapshot.val();
console.log("Snapshot : " + msgData.message);
});//end snapshot
}//end readData
my firebase data looks like. This is right under the root.
{
"-J75NmiNt3blUhVDCWbc" : {
"from" : "Blah",
"message" : "Blah"
},
"-J75N2bNbDZpshEBG1yS" : {
"from" : "Jackson",
"message" : "BLAH BlaH"
},
"-J75PCsjFlbDQ3g9vKyb" : {
"from" : "fff",
"message" : "fff"
},
"-J75MvQQpRBB6s-l3KrQ" : {
"from" : "",
"message" : ""
},
"-J75OHX7rdE1K8wpvZOt" : {
"from" : "fff",
"message" : "ff"
}
} //end
EDIT: My original answer was completely missing question, rewritten for further smiting
Are you sure that you are getting the data within your Event? It would gracefully fail. Step through the debugger and make sure you are getting your actual object passed correctly. From their example on the chat tool They are binding to an event that pushes to the listRef object like this:
$('#messageInput').keypress(function (e) {
if (e.keyCode == 13) {
var name = $('#nameInput').val();
var text = $('#messageInput').val();
messagesRef.push({name:name, text:text});
$('#messageInput').val('');
}
});
My guess is that your snapshot variable is not accessible/null.