Angular retrieving data problems ( Cannot read property 'uid' of null )












3















I'm making a web application in where I have an authorisation button for GitHub, which gets access to the personal GitHub repositories. These repositories will be displayed on the web app.



import {Component, OnInit} from '@angular/core';
import {DataService} from '../../services/data/data.service';
import {AuthService} from '../../services/auth/auth.service';
import {DatabaseService} from '../../services/share/database.service';
import {AuthdataDto} from '../../services/dto/authdata.dto';
import { map } from 'rxjs/operators';

@Component({
selector: 'app-repositories',
templateUrl: './repositories.component.html',
styleUrls: ['./repositories.component.sass']
})
export class RepositoriesComponent implements OnInit {

repositories: Object;
authData: AuthdataDto;
//loggedin: boolean;


constructor(private data: DataService, public authService:
AuthService, private dbService: DatabaseService) { }

ngOnInit() {
this.getSomething().subscribe(data => {
this.data.getrepositories(this.authService.token,
this.authService.username)});
}

getSomething(){
return this.dbService.getFromDatabase('user',
this.authService.userDetails.uid).pipe(
map((data => {this.authData = data})));
}
}


this is the error i get
I'm trying to retrieve all data first by the doSomething() method within my component and after that has been done the ngOnInit() method should get the repositories. However That is not working.



Any suggestions?



Edit: This is the AuthService class.



import {Injectable} from '@angular/core';
import * as firebase from 'firebase';
import {AngularFireAuth} from 'angularfire2/auth';
import {Router} from '@angular/router';
import {Observable, throwError} from 'rxjs';
import {UserDto} from '../dto/user.dto';
import {DatabaseService} from '../share/database.service';
import {AuthdataDto} from '../dto/authdata.dto';

@Injectable({
providedIn: 'root'
})
export class AuthService {
private _user: Observable<firebase.User>;
private _userdata: UserDto;
private _userDetails: firebase.User = null;

private _username;
private _token;

constructor(private _afAuth: AngularFireAuth, private router:
Router, private databaseService: DatabaseService) {
this._user = _afAuth.authState;
this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {
this._userDetails = null;
}
}
);
}

public loginwithGithub() {
return new Promise<any>((resolve, reject) => {
this.loginwithGithubProvider().then(
res => {
this.userdata = new
UserDto().deserialize(JSON.parse(JSON.stringify(this.userDetails)));
this.databaseService.pushToDatabase('user', 'tim',
this.userdata);
console.log(this.databaseService.getFromDatabase('user',
'kevin'));
resolve(res);
}, err => {
console.log(err);
reject(err);
});
});
}

public loginwithGithubProvider():
Promise<firebase.auth.UserCredential> {
return new Promise<any>((resolve, reject) => {
this._afAuth.auth.signInWithPopup(
new firebase.auth.GithubAuthProvider()).then(res => {
const data = new AuthdataDto(res.additionalUserInfo.username,
res.credential['accessToken'])
this.databaseService.pushToDatabase('user', res.user.uid,
data);
}, err => {
console.log(err);
reject(err);
});
});
}

public logout(): Promise<boolean | Observable<never> | never> {
return this._afAuth.auth.signOut()
.then((res) => this.router.navigate(['/'])
.catch((err) => throwError('signout failed')));
}

public get isLoggedIn(): boolean {
return this._userDetails != null;
}

get user(): Observable<firebase.User> {
return this._user;
}

get userDetails(): firebase.User {
return this._userDetails;
}


get userdata(): UserDto {
return this._userdata;
}

set userdata(value: UserDto) {
this._userdata = value;
}


get token() {
return this._token;
}

set token(value) {
this._token = value;
}


get username() {
return this._username;
}

set username(value) {
this._username = value;
}


get afAuth(): AngularFireAuth {
return this._afAuth;
}

set afAuth(value: AngularFireAuth) {
this._afAuth = value;
}
}









share|improve this question

























  • please add your relevant code here, not in an image

    – dream88
    Nov 28 '18 at 9:27











  • added code block...

    – Krelis
    Nov 28 '18 at 9:32
















3















I'm making a web application in where I have an authorisation button for GitHub, which gets access to the personal GitHub repositories. These repositories will be displayed on the web app.



import {Component, OnInit} from '@angular/core';
import {DataService} from '../../services/data/data.service';
import {AuthService} from '../../services/auth/auth.service';
import {DatabaseService} from '../../services/share/database.service';
import {AuthdataDto} from '../../services/dto/authdata.dto';
import { map } from 'rxjs/operators';

@Component({
selector: 'app-repositories',
templateUrl: './repositories.component.html',
styleUrls: ['./repositories.component.sass']
})
export class RepositoriesComponent implements OnInit {

repositories: Object;
authData: AuthdataDto;
//loggedin: boolean;


constructor(private data: DataService, public authService:
AuthService, private dbService: DatabaseService) { }

ngOnInit() {
this.getSomething().subscribe(data => {
this.data.getrepositories(this.authService.token,
this.authService.username)});
}

getSomething(){
return this.dbService.getFromDatabase('user',
this.authService.userDetails.uid).pipe(
map((data => {this.authData = data})));
}
}


this is the error i get
I'm trying to retrieve all data first by the doSomething() method within my component and after that has been done the ngOnInit() method should get the repositories. However That is not working.



Any suggestions?



Edit: This is the AuthService class.



import {Injectable} from '@angular/core';
import * as firebase from 'firebase';
import {AngularFireAuth} from 'angularfire2/auth';
import {Router} from '@angular/router';
import {Observable, throwError} from 'rxjs';
import {UserDto} from '../dto/user.dto';
import {DatabaseService} from '../share/database.service';
import {AuthdataDto} from '../dto/authdata.dto';

@Injectable({
providedIn: 'root'
})
export class AuthService {
private _user: Observable<firebase.User>;
private _userdata: UserDto;
private _userDetails: firebase.User = null;

private _username;
private _token;

constructor(private _afAuth: AngularFireAuth, private router:
Router, private databaseService: DatabaseService) {
this._user = _afAuth.authState;
this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {
this._userDetails = null;
}
}
);
}

public loginwithGithub() {
return new Promise<any>((resolve, reject) => {
this.loginwithGithubProvider().then(
res => {
this.userdata = new
UserDto().deserialize(JSON.parse(JSON.stringify(this.userDetails)));
this.databaseService.pushToDatabase('user', 'tim',
this.userdata);
console.log(this.databaseService.getFromDatabase('user',
'kevin'));
resolve(res);
}, err => {
console.log(err);
reject(err);
});
});
}

public loginwithGithubProvider():
Promise<firebase.auth.UserCredential> {
return new Promise<any>((resolve, reject) => {
this._afAuth.auth.signInWithPopup(
new firebase.auth.GithubAuthProvider()).then(res => {
const data = new AuthdataDto(res.additionalUserInfo.username,
res.credential['accessToken'])
this.databaseService.pushToDatabase('user', res.user.uid,
data);
}, err => {
console.log(err);
reject(err);
});
});
}

public logout(): Promise<boolean | Observable<never> | never> {
return this._afAuth.auth.signOut()
.then((res) => this.router.navigate(['/'])
.catch((err) => throwError('signout failed')));
}

public get isLoggedIn(): boolean {
return this._userDetails != null;
}

get user(): Observable<firebase.User> {
return this._user;
}

get userDetails(): firebase.User {
return this._userDetails;
}


get userdata(): UserDto {
return this._userdata;
}

set userdata(value: UserDto) {
this._userdata = value;
}


get token() {
return this._token;
}

set token(value) {
this._token = value;
}


get username() {
return this._username;
}

set username(value) {
this._username = value;
}


get afAuth(): AngularFireAuth {
return this._afAuth;
}

set afAuth(value: AngularFireAuth) {
this._afAuth = value;
}
}









share|improve this question

























  • please add your relevant code here, not in an image

    – dream88
    Nov 28 '18 at 9:27











  • added code block...

    – Krelis
    Nov 28 '18 at 9:32














3












3








3


0






I'm making a web application in where I have an authorisation button for GitHub, which gets access to the personal GitHub repositories. These repositories will be displayed on the web app.



import {Component, OnInit} from '@angular/core';
import {DataService} from '../../services/data/data.service';
import {AuthService} from '../../services/auth/auth.service';
import {DatabaseService} from '../../services/share/database.service';
import {AuthdataDto} from '../../services/dto/authdata.dto';
import { map } from 'rxjs/operators';

@Component({
selector: 'app-repositories',
templateUrl: './repositories.component.html',
styleUrls: ['./repositories.component.sass']
})
export class RepositoriesComponent implements OnInit {

repositories: Object;
authData: AuthdataDto;
//loggedin: boolean;


constructor(private data: DataService, public authService:
AuthService, private dbService: DatabaseService) { }

ngOnInit() {
this.getSomething().subscribe(data => {
this.data.getrepositories(this.authService.token,
this.authService.username)});
}

getSomething(){
return this.dbService.getFromDatabase('user',
this.authService.userDetails.uid).pipe(
map((data => {this.authData = data})));
}
}


this is the error i get
I'm trying to retrieve all data first by the doSomething() method within my component and after that has been done the ngOnInit() method should get the repositories. However That is not working.



Any suggestions?



Edit: This is the AuthService class.



import {Injectable} from '@angular/core';
import * as firebase from 'firebase';
import {AngularFireAuth} from 'angularfire2/auth';
import {Router} from '@angular/router';
import {Observable, throwError} from 'rxjs';
import {UserDto} from '../dto/user.dto';
import {DatabaseService} from '../share/database.service';
import {AuthdataDto} from '../dto/authdata.dto';

@Injectable({
providedIn: 'root'
})
export class AuthService {
private _user: Observable<firebase.User>;
private _userdata: UserDto;
private _userDetails: firebase.User = null;

private _username;
private _token;

constructor(private _afAuth: AngularFireAuth, private router:
Router, private databaseService: DatabaseService) {
this._user = _afAuth.authState;
this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {
this._userDetails = null;
}
}
);
}

public loginwithGithub() {
return new Promise<any>((resolve, reject) => {
this.loginwithGithubProvider().then(
res => {
this.userdata = new
UserDto().deserialize(JSON.parse(JSON.stringify(this.userDetails)));
this.databaseService.pushToDatabase('user', 'tim',
this.userdata);
console.log(this.databaseService.getFromDatabase('user',
'kevin'));
resolve(res);
}, err => {
console.log(err);
reject(err);
});
});
}

public loginwithGithubProvider():
Promise<firebase.auth.UserCredential> {
return new Promise<any>((resolve, reject) => {
this._afAuth.auth.signInWithPopup(
new firebase.auth.GithubAuthProvider()).then(res => {
const data = new AuthdataDto(res.additionalUserInfo.username,
res.credential['accessToken'])
this.databaseService.pushToDatabase('user', res.user.uid,
data);
}, err => {
console.log(err);
reject(err);
});
});
}

public logout(): Promise<boolean | Observable<never> | never> {
return this._afAuth.auth.signOut()
.then((res) => this.router.navigate(['/'])
.catch((err) => throwError('signout failed')));
}

public get isLoggedIn(): boolean {
return this._userDetails != null;
}

get user(): Observable<firebase.User> {
return this._user;
}

get userDetails(): firebase.User {
return this._userDetails;
}


get userdata(): UserDto {
return this._userdata;
}

set userdata(value: UserDto) {
this._userdata = value;
}


get token() {
return this._token;
}

set token(value) {
this._token = value;
}


get username() {
return this._username;
}

set username(value) {
this._username = value;
}


get afAuth(): AngularFireAuth {
return this._afAuth;
}

set afAuth(value: AngularFireAuth) {
this._afAuth = value;
}
}









share|improve this question
















I'm making a web application in where I have an authorisation button for GitHub, which gets access to the personal GitHub repositories. These repositories will be displayed on the web app.



import {Component, OnInit} from '@angular/core';
import {DataService} from '../../services/data/data.service';
import {AuthService} from '../../services/auth/auth.service';
import {DatabaseService} from '../../services/share/database.service';
import {AuthdataDto} from '../../services/dto/authdata.dto';
import { map } from 'rxjs/operators';

@Component({
selector: 'app-repositories',
templateUrl: './repositories.component.html',
styleUrls: ['./repositories.component.sass']
})
export class RepositoriesComponent implements OnInit {

repositories: Object;
authData: AuthdataDto;
//loggedin: boolean;


constructor(private data: DataService, public authService:
AuthService, private dbService: DatabaseService) { }

ngOnInit() {
this.getSomething().subscribe(data => {
this.data.getrepositories(this.authService.token,
this.authService.username)});
}

getSomething(){
return this.dbService.getFromDatabase('user',
this.authService.userDetails.uid).pipe(
map((data => {this.authData = data})));
}
}


this is the error i get
I'm trying to retrieve all data first by the doSomething() method within my component and after that has been done the ngOnInit() method should get the repositories. However That is not working.



Any suggestions?



Edit: This is the AuthService class.



import {Injectable} from '@angular/core';
import * as firebase from 'firebase';
import {AngularFireAuth} from 'angularfire2/auth';
import {Router} from '@angular/router';
import {Observable, throwError} from 'rxjs';
import {UserDto} from '../dto/user.dto';
import {DatabaseService} from '../share/database.service';
import {AuthdataDto} from '../dto/authdata.dto';

@Injectable({
providedIn: 'root'
})
export class AuthService {
private _user: Observable<firebase.User>;
private _userdata: UserDto;
private _userDetails: firebase.User = null;

private _username;
private _token;

constructor(private _afAuth: AngularFireAuth, private router:
Router, private databaseService: DatabaseService) {
this._user = _afAuth.authState;
this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {
this._userDetails = null;
}
}
);
}

public loginwithGithub() {
return new Promise<any>((resolve, reject) => {
this.loginwithGithubProvider().then(
res => {
this.userdata = new
UserDto().deserialize(JSON.parse(JSON.stringify(this.userDetails)));
this.databaseService.pushToDatabase('user', 'tim',
this.userdata);
console.log(this.databaseService.getFromDatabase('user',
'kevin'));
resolve(res);
}, err => {
console.log(err);
reject(err);
});
});
}

public loginwithGithubProvider():
Promise<firebase.auth.UserCredential> {
return new Promise<any>((resolve, reject) => {
this._afAuth.auth.signInWithPopup(
new firebase.auth.GithubAuthProvider()).then(res => {
const data = new AuthdataDto(res.additionalUserInfo.username,
res.credential['accessToken'])
this.databaseService.pushToDatabase('user', res.user.uid,
data);
}, err => {
console.log(err);
reject(err);
});
});
}

public logout(): Promise<boolean | Observable<never> | never> {
return this._afAuth.auth.signOut()
.then((res) => this.router.navigate(['/'])
.catch((err) => throwError('signout failed')));
}

public get isLoggedIn(): boolean {
return this._userDetails != null;
}

get user(): Observable<firebase.User> {
return this._user;
}

get userDetails(): firebase.User {
return this._userDetails;
}


get userdata(): UserDto {
return this._userdata;
}

set userdata(value: UserDto) {
this._userdata = value;
}


get token() {
return this._token;
}

set token(value) {
this._token = value;
}


get username() {
return this._username;
}

set username(value) {
this._username = value;
}


get afAuth(): AngularFireAuth {
return this._afAuth;
}

set afAuth(value: AngularFireAuth) {
this._afAuth = value;
}
}






angular






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 9:43







Krelis

















asked Nov 28 '18 at 9:26









KrelisKrelis

235




235













  • please add your relevant code here, not in an image

    – dream88
    Nov 28 '18 at 9:27











  • added code block...

    – Krelis
    Nov 28 '18 at 9:32



















  • please add your relevant code here, not in an image

    – dream88
    Nov 28 '18 at 9:27











  • added code block...

    – Krelis
    Nov 28 '18 at 9:32

















please add your relevant code here, not in an image

– dream88
Nov 28 '18 at 9:27





please add your relevant code here, not in an image

– dream88
Nov 28 '18 at 9:27













added code block...

– Krelis
Nov 28 '18 at 9:32





added code block...

– Krelis
Nov 28 '18 at 9:32












2 Answers
2






active

oldest

votes


















0














check your .subscribe(). If you peer close you might notice that "data" is grayed out, meaning it's never used.






share|improve this answer
























  • How could that be an unused parameter? That's the part I don't get. So what in this case; am I doing wrong?

    – Krelis
    Nov 28 '18 at 9:37



















0














The error seems to be clear, "cannot read prorperty 'uid' of null"
Inside your "getSomething" you are calling "this.authService.userDetails.uid" where "userDetails" is null, can you check if "userDetails" have a value ?



EDIT



In your service you are getting User but user is null so you are setting userDetails null too, look here :



this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {

// Put a breakpoint here
this._userDetails = null;
}
}


EDIT 2



Ok I suggest you to make your _user public or create a new subject which represent the original _user observable ( but it's already what you are doing by setting "_user = afAuth.authState");



If you do this, you will able to know the state of the observable and then wait for you "user" data to be retrieved before call other methods.



You can use the "switchMap" operator like this :



Component



getSomething(){
return this.authService.user$.pipe(
switchMap(userData =>
this.dbService.getFromDatabase('user', userData.uid)),
map(data => {this.authData = data}));
}


}



marble up here => https://rxviz.com/v/0oqMeYwo






share|improve this answer


























  • Added the code block for that part

    – Krelis
    Nov 28 '18 at 9:43











  • @Krelis answer updated

    – Kevin ALBRECHT
    Nov 28 '18 at 9:51













  • users are retrieved async from firebase, the RepositoriesComponent should wait until the user data is retrieved. Until then it returns null. I want RepositoriesComponent to check and wait for user data first and then after it has retrieved that data, then do - getRepositories() with the given userdetails.

    – Krelis
    Nov 28 '18 at 9:55













  • @Krelis again updated, because you have dependency between 2 service composed by privates observable it's hard to tell when one is finished, so the goal is to define an accessible reference to the observable without re-execute any recuperation of data but just chain your exec with your dependencies

    – Kevin ALBRECHT
    Nov 28 '18 at 11:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516096%2fangular-retrieving-data-problems-cannot-read-property-uid-of-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














check your .subscribe(). If you peer close you might notice that "data" is grayed out, meaning it's never used.






share|improve this answer
























  • How could that be an unused parameter? That's the part I don't get. So what in this case; am I doing wrong?

    – Krelis
    Nov 28 '18 at 9:37
















0














check your .subscribe(). If you peer close you might notice that "data" is grayed out, meaning it's never used.






share|improve this answer
























  • How could that be an unused parameter? That's the part I don't get. So what in this case; am I doing wrong?

    – Krelis
    Nov 28 '18 at 9:37














0












0








0







check your .subscribe(). If you peer close you might notice that "data" is grayed out, meaning it's never used.






share|improve this answer













check your .subscribe(). If you peer close you might notice that "data" is grayed out, meaning it's never used.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 9:32









CharlieCharlie

108210




108210













  • How could that be an unused parameter? That's the part I don't get. So what in this case; am I doing wrong?

    – Krelis
    Nov 28 '18 at 9:37



















  • How could that be an unused parameter? That's the part I don't get. So what in this case; am I doing wrong?

    – Krelis
    Nov 28 '18 at 9:37

















How could that be an unused parameter? That's the part I don't get. So what in this case; am I doing wrong?

– Krelis
Nov 28 '18 at 9:37





How could that be an unused parameter? That's the part I don't get. So what in this case; am I doing wrong?

– Krelis
Nov 28 '18 at 9:37













0














The error seems to be clear, "cannot read prorperty 'uid' of null"
Inside your "getSomething" you are calling "this.authService.userDetails.uid" where "userDetails" is null, can you check if "userDetails" have a value ?



EDIT



In your service you are getting User but user is null so you are setting userDetails null too, look here :



this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {

// Put a breakpoint here
this._userDetails = null;
}
}


EDIT 2



Ok I suggest you to make your _user public or create a new subject which represent the original _user observable ( but it's already what you are doing by setting "_user = afAuth.authState");



If you do this, you will able to know the state of the observable and then wait for you "user" data to be retrieved before call other methods.



You can use the "switchMap" operator like this :



Component



getSomething(){
return this.authService.user$.pipe(
switchMap(userData =>
this.dbService.getFromDatabase('user', userData.uid)),
map(data => {this.authData = data}));
}


}



marble up here => https://rxviz.com/v/0oqMeYwo






share|improve this answer


























  • Added the code block for that part

    – Krelis
    Nov 28 '18 at 9:43











  • @Krelis answer updated

    – Kevin ALBRECHT
    Nov 28 '18 at 9:51













  • users are retrieved async from firebase, the RepositoriesComponent should wait until the user data is retrieved. Until then it returns null. I want RepositoriesComponent to check and wait for user data first and then after it has retrieved that data, then do - getRepositories() with the given userdetails.

    – Krelis
    Nov 28 '18 at 9:55













  • @Krelis again updated, because you have dependency between 2 service composed by privates observable it's hard to tell when one is finished, so the goal is to define an accessible reference to the observable without re-execute any recuperation of data but just chain your exec with your dependencies

    – Kevin ALBRECHT
    Nov 28 '18 at 11:22
















0














The error seems to be clear, "cannot read prorperty 'uid' of null"
Inside your "getSomething" you are calling "this.authService.userDetails.uid" where "userDetails" is null, can you check if "userDetails" have a value ?



EDIT



In your service you are getting User but user is null so you are setting userDetails null too, look here :



this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {

// Put a breakpoint here
this._userDetails = null;
}
}


EDIT 2



Ok I suggest you to make your _user public or create a new subject which represent the original _user observable ( but it's already what you are doing by setting "_user = afAuth.authState");



If you do this, you will able to know the state of the observable and then wait for you "user" data to be retrieved before call other methods.



You can use the "switchMap" operator like this :



Component



getSomething(){
return this.authService.user$.pipe(
switchMap(userData =>
this.dbService.getFromDatabase('user', userData.uid)),
map(data => {this.authData = data}));
}


}



marble up here => https://rxviz.com/v/0oqMeYwo






share|improve this answer


























  • Added the code block for that part

    – Krelis
    Nov 28 '18 at 9:43











  • @Krelis answer updated

    – Kevin ALBRECHT
    Nov 28 '18 at 9:51













  • users are retrieved async from firebase, the RepositoriesComponent should wait until the user data is retrieved. Until then it returns null. I want RepositoriesComponent to check and wait for user data first and then after it has retrieved that data, then do - getRepositories() with the given userdetails.

    – Krelis
    Nov 28 '18 at 9:55













  • @Krelis again updated, because you have dependency between 2 service composed by privates observable it's hard to tell when one is finished, so the goal is to define an accessible reference to the observable without re-execute any recuperation of data but just chain your exec with your dependencies

    – Kevin ALBRECHT
    Nov 28 '18 at 11:22














0












0








0







The error seems to be clear, "cannot read prorperty 'uid' of null"
Inside your "getSomething" you are calling "this.authService.userDetails.uid" where "userDetails" is null, can you check if "userDetails" have a value ?



EDIT



In your service you are getting User but user is null so you are setting userDetails null too, look here :



this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {

// Put a breakpoint here
this._userDetails = null;
}
}


EDIT 2



Ok I suggest you to make your _user public or create a new subject which represent the original _user observable ( but it's already what you are doing by setting "_user = afAuth.authState");



If you do this, you will able to know the state of the observable and then wait for you "user" data to be retrieved before call other methods.



You can use the "switchMap" operator like this :



Component



getSomething(){
return this.authService.user$.pipe(
switchMap(userData =>
this.dbService.getFromDatabase('user', userData.uid)),
map(data => {this.authData = data}));
}


}



marble up here => https://rxviz.com/v/0oqMeYwo






share|improve this answer















The error seems to be clear, "cannot read prorperty 'uid' of null"
Inside your "getSomething" you are calling "this.authService.userDetails.uid" where "userDetails" is null, can you check if "userDetails" have a value ?



EDIT



In your service you are getting User but user is null so you are setting userDetails null too, look here :



this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {

// Put a breakpoint here
this._userDetails = null;
}
}


EDIT 2



Ok I suggest you to make your _user public or create a new subject which represent the original _user observable ( but it's already what you are doing by setting "_user = afAuth.authState");



If you do this, you will able to know the state of the observable and then wait for you "user" data to be retrieved before call other methods.



You can use the "switchMap" operator like this :



Component



getSomething(){
return this.authService.user$.pipe(
switchMap(userData =>
this.dbService.getFromDatabase('user', userData.uid)),
map(data => {this.authData = data}));
}


}



marble up here => https://rxviz.com/v/0oqMeYwo







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '18 at 11:18

























answered Nov 28 '18 at 9:37









Kevin ALBRECHTKevin ALBRECHT

369111




369111













  • Added the code block for that part

    – Krelis
    Nov 28 '18 at 9:43











  • @Krelis answer updated

    – Kevin ALBRECHT
    Nov 28 '18 at 9:51













  • users are retrieved async from firebase, the RepositoriesComponent should wait until the user data is retrieved. Until then it returns null. I want RepositoriesComponent to check and wait for user data first and then after it has retrieved that data, then do - getRepositories() with the given userdetails.

    – Krelis
    Nov 28 '18 at 9:55













  • @Krelis again updated, because you have dependency between 2 service composed by privates observable it's hard to tell when one is finished, so the goal is to define an accessible reference to the observable without re-execute any recuperation of data but just chain your exec with your dependencies

    – Kevin ALBRECHT
    Nov 28 '18 at 11:22



















  • Added the code block for that part

    – Krelis
    Nov 28 '18 at 9:43











  • @Krelis answer updated

    – Kevin ALBRECHT
    Nov 28 '18 at 9:51













  • users are retrieved async from firebase, the RepositoriesComponent should wait until the user data is retrieved. Until then it returns null. I want RepositoriesComponent to check and wait for user data first and then after it has retrieved that data, then do - getRepositories() with the given userdetails.

    – Krelis
    Nov 28 '18 at 9:55













  • @Krelis again updated, because you have dependency between 2 service composed by privates observable it's hard to tell when one is finished, so the goal is to define an accessible reference to the observable without re-execute any recuperation of data but just chain your exec with your dependencies

    – Kevin ALBRECHT
    Nov 28 '18 at 11:22

















Added the code block for that part

– Krelis
Nov 28 '18 at 9:43





Added the code block for that part

– Krelis
Nov 28 '18 at 9:43













@Krelis answer updated

– Kevin ALBRECHT
Nov 28 '18 at 9:51







@Krelis answer updated

– Kevin ALBRECHT
Nov 28 '18 at 9:51















users are retrieved async from firebase, the RepositoriesComponent should wait until the user data is retrieved. Until then it returns null. I want RepositoriesComponent to check and wait for user data first and then after it has retrieved that data, then do - getRepositories() with the given userdetails.

– Krelis
Nov 28 '18 at 9:55







users are retrieved async from firebase, the RepositoriesComponent should wait until the user data is retrieved. Until then it returns null. I want RepositoriesComponent to check and wait for user data first and then after it has retrieved that data, then do - getRepositories() with the given userdetails.

– Krelis
Nov 28 '18 at 9:55















@Krelis again updated, because you have dependency between 2 service composed by privates observable it's hard to tell when one is finished, so the goal is to define an accessible reference to the observable without re-execute any recuperation of data but just chain your exec with your dependencies

– Kevin ALBRECHT
Nov 28 '18 at 11:22





@Krelis again updated, because you have dependency between 2 service composed by privates observable it's hard to tell when one is finished, so the goal is to define an accessible reference to the observable without re-execute any recuperation of data but just chain your exec with your dependencies

– Kevin ALBRECHT
Nov 28 '18 at 11:22


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516096%2fangular-retrieving-data-problems-cannot-read-property-uid-of-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks