如何使用Javascript在Cloud Firestore中创建集合?

Tom*_*.ST 4 firebase firebase-authentication firebase-realtime-database angular google-cloud-firestore

当用户在我的角度应用程序中创建帐户时,我的应用程序使用电子邮件和密码创建用户帐户(使用Firebase API)。但我也想创建一个新集合来保存更多数据,例如姓名,姓氏...

每个集合的名称将具有uid。但我不知道该如何强迫? db.createCollection('abcXdefX')不存在。我不知道该怎么办。请帮忙。

Gre*_*g_M 5

您不能创建空集合,而必须在其中创建文档。 数据模型

function writeUserData(userId, name, email, imageUrl) {
      firebase.database().ref('users/' + userId).set({
        username: name,
        email: email,
        profile_picture : imageUrl
      });
    }
Run Code Online (Sandbox Code Playgroud)

您可以通过这种方式获取用户ID

var userId = firebase.auth().currentUser.uid;
Run Code Online (Sandbox Code Playgroud)

对于云消防站

db.doc(userId + '/data').set({
  name: name,
  email: email
});
Run Code Online (Sandbox Code Playgroud)

firebase doc-读写

  • 您不能创建空集合,因此您需要创建一些文档,例如`db.collection(userId).doc("data").set({ name: "username" })` (2认同)