为什么我无法以这种方式将数据添加到Firebase中?

时间:2017-03-06 21:22:30

标签: ios firebase swift3 firebase-realtime-database

我正在做一个关注Firebase的项目,我尝试将数据上传到Firebase,作为Firebase网站教程中的代码:

    // An index to track Ada's memberships
{
  "users": {
    "alovelace": {
      "name": "Ada Lovelace",
      // Index Ada's groups in her profile
      "groups": {
         // the value here doesn't matter, just that the key exists
         "techpioneers": true,
         "womentechmakers": true
      }
    },
    ...
  },
  "groups": {
    "techpioneers": {
      "name": "Historical Tech Pioneers",
      "members": {
        "alovelace": true,
        "ghopper": true,
        "eclarke": true
      }
    },
    ...
  }
}

我遵循这个结构来编写我自己的数据结构,如下所示:

func pushUser() {
        let username = self.username
        let userid = FIRAuth.auth()?.currentUser?.uid

        let userInfo : [String : Any] = ["username" : username,
            "userID" : userid,
            "sggestedRoutes" : "",
            "myTracedRoutes" : "",
            "routes": {
                "usualInfo" : true,
                "calcuInfo" : true
        }
    ]

        let userRef = FIRDatabase.database().reference()
        userRef.child("user").childByAutoId().setValue(userInfo)
    }
在Swift3中

,但调试器说嵌套路由{}不正确,我对此很困惑,有人能给我一个提示吗?

亲切的问候

收到建议后,我对代码进行了一些更改,但它仍然没有在Route部分上工作:(

func pushUser() {
        let username = self.username
        let userid = FIRAuth.auth()?.currentUser?.uid

        let userInfo : [String : Any] =
            ["username" : username,
             "userID" : userid,
             "sggestedRoutes" : "",
             "uploadedRoutes" : "",
             "myTracedRoutes" : "",
             ["Routes":
                ["usualInfo" : true,
                 "calcuInfo" : true]
             ]
            ]
        let userRef = FIRDatabase.database().reference()
        userRef.child("user").childByAutoId().setValue(userInfo)
    }

1 个答案:

答案 0 :(得分:1)

尝试将路线添加为[“routes”:[“usualInfo”:true,“calcuInfo”:true]]

不要忘记两个孩子“用户信息”和“路线”的正确关闭