Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get the uid generated by Firebase? #1278

Closed
EduardoIbarra opened this issue Oct 18, 2017 · 8 comments
Closed

How can I get the uid generated by Firebase? #1278

EduardoIbarra opened this issue Oct 18, 2017 · 8 comments

Comments

@EduardoIbarra
Copy link

Version info

Angular: 4.4.5

Firebase:

AngularFire: angularfire2

This is not an issue, I just want to know how do I get to these kind of uid generated by Firebase? I'm trying to get a document, but since I don't have its id, I cannot reach it, also, I cannot query it, and if i use a collection to be able to query it, it just won't let me update or delete the record.

** Screenshots **
screen shot 2017-10-17 at 7 42 15 pm

@bob-lee
Copy link
Contributor

bob-lee commented Oct 18, 2017

https://stackblitz.com/edit/angular-iqq9xo
Above demo would show you how to.. Better ask this type of question in StackOverflow

@EduardoIbarra
Copy link
Author

Thanks! But my elements' .$key gives me undefined. Am I doing this right? This is the code on my service to retrieve all records:

getGames() {
    return this.afDB.collection('games');
}

And this is my code on my component:

gamesService.getGames()
            .valueChanges().subscribe((games) => {
            this.games = games;
        }, (error) => {
            console.log(error);
        });

This is how I access them on my html:
<tr *ngFor="let game of games">

@blackbear003
Copy link

blackbear003 commented Oct 18, 2017

const id = this.afs.createId();
this.xDocument = this.afs.doc(uid + '/' + id);
x.key = id;
return this.xDocument.set(x)

@EduardoIbarra
Copy link
Author

Thank you both! My code ended up like this:

import { Injectable } from '@angular/core';
import {AngularFirestore, AngularFirestoreCollection} from 'angularfire2/firestore';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class GamesFirebaseService {
    private itemsCollection: AngularFirestoreCollection<any>;
    items: Observable<any[]>;
    countItems = 0;
    constructor(public afs: AngularFirestore) {
        this.itemsCollection = this.afs.collection<any>('games');
        this.items = this.itemsCollection.snapshotChanges()
            .map(actions => {
                this.countItems = actions.length;
                return actions.map(action => ({ $key: action.payload.doc.id, ...action.payload.doc.data() }));
            });
    }
    public store = (game) => {
        return this.itemsCollection.add(game);
    }
    public update(game) {
        return this.itemsCollection.doc(game.$key).update(game);
    }
}

@cbstodd
Copy link

cbstodd commented Jul 9, 2018

Thank you for posting your code @EduardoIbarra. Helped big time.

@anuj9196
Copy link

anuj9196 commented Mar 7, 2019

screenshot 2019-03-07 at 11 33 28 pm

I'm getting this error Unresolved map().

@hanyska
Copy link

hanyska commented May 22, 2019

screenshot 2019-03-07 at 11 33 28 pm

I'm getting this error Unresolved map().

use .pipe( map(...))

@TusharKalyankar
Copy link

You can relate to the below example and generate your code

Here "_email","_password","_city","_phone" are variables for

onSaved: (value) => _email = value

taken from TextFormField() function

Code:

String userId = await widget.auth.createUserWithEmailAndPassword(_email, _password);

print('Registered user: $userId');

final new_user = await FirebaseFirestore.instance.collection('users').doc(userId).
set({"UserUID":'$userId',"Email":_email,"Password":_password,"City":_city,"Phone
Number":_phone});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants