Hi I am not able to get single record data and its not throwing any error. Please help me out.
My package.json
"dependencies": {
"@angular/animations": "^6.0.3",
"@angular/common": "^6.0.3",
"@angular/compiler": "^6.0.3",
"@angular/core": "^6.0.3",
"@angular/forms": "^6.0.3",
"@angular/http": "^6.0.3",
"@angular/platform-browser": "^6.0.3",
"@angular/platform-browser-dynamic": "^6.0.3",
"@angular/router": "^6.0.3",
"angularfire2": "^5.0.0-rc.6.0",
"core-js": "^2.5.4",
"firebase": "^5.3.1",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.3",
"@angular-devkit/build-angular": "~0.6.8",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.8",
"@angular/language-service": "^6.0.3",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.0",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
}
My controller file.
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { UserService } from '../user.service';
@Component({
selector: 'app-user-details',
templateUrl: './user-details.component.html',
styleUrls: ['./user-details.component.css']
})
export class UserDetailsComponent implements OnInit {
id: any;
userList: any;
imageUrl: any;
constructor(private _user: UserService, private router: Router, private arouter: ActivatedRoute) { }
ngOnInit() {
this.id = this.arouter.snapshot.params['id'];
console.log(this.id);
this._user.getUserDetails(this.id);
console.log(this.userList);
}
}
My service file
import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import { User } from './user';
import * as firebase from 'firebase';
@Injectable({
providedIn: 'root'
})
export class UserService {
userList: AngularFireList
folder: any;
userDetails: Observable
constructor(private _fb: AngularFireDatabase) {
this.folder = 'userProfile';
this.userList = _fb.list('/user');
}
getAllUser() {
return this.userList;
}
inserUser(contFrm: User){
this.userList.push(contFrm);
}
getUserDetails(id) {
console.log('user/'+id);
this.userDetails = this._fb.object('user/'+id).snapshotChanges().map(res => {
let x = res.payload.val();
});
}
}
Your map() function is wrong since it does not return anything, i.e. the return value will be undefined. Check this sample on Stackblitz.com to see a working example. Please remember to add your Firebase config in the app.module.ts file.
Change your map() function to:
this.userDetails = this._fb.object('user/'+id).snapshotChanges().map(res => res.payload.val());
and since you are using RxJS 6, I would suggest using pipe(), i.e.:
this.userDetails = this._fb.object('user/'+id).snapshotChanges().pipe(
map(res => res.payload.val())
);
and please note that both object() and list() methods have valueChanges() which spares you from mapping the snapshot to the actual value, so the above can be achieved using only:
this.userDetails = this._fb.object('user/'+id).valueChanges();
Above solution is working for me. thanks @kanafghan
Your welcome @vhosamane, I'm glad that I could help.
Most helpful comment
Your
map()function is wrong since it does not return anything, i.e. the return value will beundefined. Check this sample on Stackblitz.com to see a working example. Please remember to add your Firebase config in theapp.module.tsfile.Change your
map()function to:and since you are using RxJS 6, I would suggest using
pipe(), i.e.:and please note that both
object()andlist()methods havevalueChanges()which spares you from mapping the snapshot to the actual value, so the above can be achieved using only: