简体   繁体   English

如何在Firebase中创建多个子代

[英]How to create multiple childs in Firebase

I am working on a project where I need to send data to firebase an a specific manner: 我正在一个需要以特定方式将数据发送到Firebase的项目中:

var ref = firebase.database().ref('Codes').child(firebaseUser.uid).child(className);

However, I can not send the data using this piece of code. 但是,我无法使用这段代码发送数据。 How do I send data through two child locations. 如何通过两个子位置发送数据。

I faced such kind of problems while learning firebase but I managed like this in Kotlin. 我在学习Firebase时遇到了这类问题,但我在Kotlin设法做到了。

var database: DatabaseReference = FirebaseDatabase.getInstance().reference
                var userClass = User()
                userClass.id = user.uid
                userClass.name = user.displayName
                userClass.email = user.email
                userClass.imageUrl = user.photoUrl?.toString()
                userClass.phoneNumber = user.phoneNumber
                database.child("users").child(user.uid).setValue(userClass)

firebase.child returns FireBaseReference setValue puts the value to the reference.Hope this will help. firebase.child返回FireBaseReference setValue将值放入引用中。希望这会有所帮助。

To write an object to a location: 要将对象写入位置:

var ref = firebase.database().ref('Codes').child(firebaseUser.uid).child(className);
ref.set({ key1: "value1", key2: true })

To write values to two completely different paths in the database, use complete paths as the keys: 要将值写入数据库中两个完全不同的路径,请使用完整路径作为键:

var ref = firebase.database().ref();
var updates = {};
updates["Codes/"+firebaseUser.uid+"/"+className] = "value1";
updates["Otherpath/child/key2"] = true
ref.set(updates)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM