2016-08-17 1 views
1

firebase DBにあるユーザーの特定の値を保存しようとしています。私がデータを理解できる唯一の方法は、userIDをキーとして使用することです。ここで私はfirebaseに値を格納する際にuserIDをキーとして使用できません

https://firebaseDB/user_orders/userID達成しようとしているものです - >リスト(注文)を

しかし、私はキーとしてユーザーIDを使用しようとすると、私は

2016-を言って、エラーを取得しています0835 17:56:10.479 BetMe [4322:84427] *** 未知の例外 'NSUnknownKeyException'の理由でアプリケーションを終了しています:理由: '[setValue:forUndefinedKey:]: このクラスは、キー 3ISnq916LBdmmv7330ELByxCHV33 '

は、ここに私のコードスニペットで

let orderRef = rootRef.child("user_orders") 

     orderRef.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in 
      // 
      if(!snapshot.hasChild(self.currentUser.uid)) 
      { 
       orderRef.setValue("", forKey: self.currentUser.uid) 
      } 

      orderRef.child(self.currentUser.uid) 
     }) 

     orderRef.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in 
      for order in orders 
      { 
       // 
       if(!snapshot.hasChild(order.betId)){ 
        orderRef.setValue(order.dictionaryRepresentation(), forKey: order.betId) 
       } 

      } 
     }) 

私を助けてください!

答えて

2

私は何を記録しているのか分かりませんが、私は空白を保存しました。問題は、まだ存在しないキーに保存しようとしていることだと思います。

orderRef.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in 
     // 
     if(!snapshot.hasChild(self.currentUser.uid)) 
     { 
      let blankOrder = ["orderID": ""] 
      //setting the uid as a path should create it when setValue is called 
      orderRef.child(self.currentUser.uid).setValue(blankOrder) 
     } 
    }) 
+0

どうもありがとう :) – Kanishka

関連する問題