2

I have users data stored in firebase as

users : {
    -KamJGmnL8S4Nn_okIcc : {
        name: "Someone",
        email: "example.com",
        website: "somesite.com"
    },
    -laeghmnw8S4Nn_9inb47 : {
        name: "Someone",
        email: "example.com",
        website: "somesite.com"
    }
}

I need to add a new Object { collections: <key> : { ... }, <key>: { ... } } to an user, say the first user in the above object. I need to be able to push to collections as it can have several objects.

How can i do this with angularfire2?

1 Answer 1

2

You could use a list to push them individually:

const uid = "-KamJGmnL8S4Nn_okIcc";
const list = angularFire.database.list(`users/${uid}/collections`);
list.push({ name: "collection-a" });
list.push({ name: "collection-b" });

Or you could use an object to update them together (in a multi-location update):

const uid = "-KamJGmnL8S4Nn_okIcc";
const obj = angularFire.database.object(`users/${uid}`);

let collections = {
    `collections/${obj.$ref.push()}/name`: "collection-a",
    `collections/${obj.$ref.push()}/name`: "collection-b",
};
obj.update(collections);

Note that push keys are generated on the client and you can generate one by calling push with no arguments.

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

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.