How to create collection in Cloud Firestore from Javascript?

℡╲_俬逩灬. 提交于 2019-12-10 15:20:59

问题


When user creates account in my angular app, my app creates user account (using Firebase API) with email and password. But also I want to create new collection to hold more data like name, surname...

Each collection's name will have uid. But I don't know how to force it? db.createCollection('abcXdefX') doesn't exist. I've no idea what to do. Please, help.


回答1:


You cannot create empty collection, you have to create document inside. data model

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

You can get user id in this way

var userId = firebase.auth().currentUser.uid;

For cloud firestore

db.doc(userId + '/data').set({
  name: name,
  email: email
});

firebase doc - read&write



来源:https://stackoverflow.com/questions/47871288/how-to-create-collection-in-cloud-firestore-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!