Swift Firebase Must Be a Non-Empty String and Not Contain '.' '#' '$' '[' or ']'

Receiving a Firebase snapshot from a child with an array SWIFT

Try This

var post = [String]()
ref.observe(.value, with: { (snapshot) in
for item in snapshot.children{
self.post.append((item as AnyObject).key)
}
})

Then you print "post" and you will get ["170802120618", "170802101427"]

How do i check if snapshot contains a specific value in Firebase?

You specify your problem in your text. An email always contains a dot '.'. That is not allowed in the key. You check if the key contains the email, but that is not allowed. Structure your database like this:

"email": theemail

This way your key is "email" and your value is theemail. In the value, every string is allowed.

self.databaseRef.child("Test").child("xxx").observeSingleEvent(of: .value, with: { (snapshot) in
let values = snapshot.value as? NSDictionary
let email = values?["email"] as? String ?? "No email found"
// OR
if let email = values?["email"] as? String{
//email (above declared variable) holds the value (the real email)
}else{
//no email
}
if email == (user!.email)!{

}else{
}
})

Adding Firebase data, dots and forward slashes

The reason that adding a child 02/10/2013 creates a structure in Firebase is because the forward slashes are resulting in the creation of a new level.

So the line I assume you are using something similar to: firebaseRef.child('02/10/2013').set(true) is equivalent to firebaseRef.child('02').child('10').child('2013').set(true).

To avoid the problems of not being able to use the following characters in reference key names (source),

  • . (period)
  • $ (dollar sign)
  • [ (left square bracket)
  • ] (right square bracket)
  • # (hash or pound sign)
  • / (forward slash)

we can use one of JavaScript's built in encoding functions since as far as I can tell, Firebase does not provide a built in method to do so. Here's a run-through to see which is the most effective for our purposes:

var forbiddenChars = '.$[]#/'; //contains the forbidden characters
escape(forbiddenChars); //results in ".%24%5B%5D%23/"
encodeURI(forbiddenChars); //results in ".%24%5B%5D%23%2F"
encodeURIComponent(forbiddenChars); //results in ".%24%5B%5D%23%2F"

Evidently, the most effective solution is encodeURIComponent. However, it doesn't solve all our problems. The . character still poses a problem as shown by the above test and trying to encodeURIComponent your test e-mail address. My suggestion would be to chain a replace function after the encodeURIComponent to deal with the periods.

Here's what the solution would look like for your two example cases:

encodeURIComponent('Henry.Morgan@caribbean.sea').replace(/\./g, '%2E') //results in "Henry%2EMorgan%40caribbean%2Esea"
encodeURIComponent('02/10/2013'); //results in "02%2F10%2F2013"

Since both the final results are safe for insertion into a Firebase as a key name, the only other concern is decoding after reading from a Firebase which can be solved with replace('%2E', '.') and a simple decodeURIComponent(...).



Related Topics



Leave a reply



Submit