9

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.

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer, but I don't want to have a "user" collection. Name of the collection is userId.
So change the path to this: firebase.database().ref(userId)
You can't create empty collection so you need to create some doc e.g db.collection(userId).doc("data").set({ name: "username" })
3

I was trying to create a collection within a document and the accepted answer didn't work for me, but this did:

db
    .collection("coll1")
    .doc("doc1")
    .collection("newCollectionName")
    .doc("newDocName")
    .set(data);

Comments

1

In Firestore, when you add a document to a non-existent collection, the collection gets created without needing to specifically create it. For example,

firebase.firestore().collection("new-coll").add({"Hello":"World"})

creates the collection new-coll if it does not exist and adds the document {"Hello": "World"} to it.

Comments

0

You can also create a document inside the collection. You don't have to create a different collection every time.

db.collection("users").doc("userid").set({
    username: 'username',
    password: 'password', 
    email: 'email', 
    age: 'age' 
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.