Angularfire: How i can get single record with ID

Created on 5 Sep 2017  路  6Comments  路  Source: angular/angularfire

`
surveys2: FirebaseListObservable;
constructor(public afAuth: AngularFireAuth, public af: AngularFireDatabase) {

this.surveys2 =af.list('surveys', {
query: {
equalTo: '-Kss5Jp2IlnBWiK6V4MC'
}
});

}`
i'm doing it for getting the single record but it's not working can some one tell me what's the issue in view i'm parsing it like that
{{surverys2 | async | json}}

but print nothing null id is valid in firebase database.

Most helpful comment

af.object('surveys/-KtG6W10NVSUn5V17eRy') gives you an item as a single object
See here https://github.com/angular/angularfire2/blob/master/docs/2-retrieving-data-as-objects.md
For more info

All 6 comments

af.object('surveys/-KtG6W10NVSUn5V17eRy') gives you an item as a single object
See here https://github.com/angular/angularfire2/blob/master/docs/2-retrieving-data-as-objects.md
For more info

You can use this using current angularfire2 version like below

controller.ts

import { Observable } from 'rxjs/Observable';
.......
surveys2 : Observable<any>;

constructor(public db: AngularFireDatabase) {
    this.surveys2 = this.db.object('surveys/-Kss5Jp2IlnBWiK6V4MC')
        .snapshotChanges().map(res => {
            return res.payload.val();
        });
}

html

{{surveys2 |async|json}}

I am also facing same issue. I am not able to get single value.

My ts file passing id to get single record.

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();
});
}

}

af.object('surveys/-KtG6W10NVSUn5V17eRy') gives you an item as a single object
See here https://github.com/angular/angularfire2/blob/master/docs/2-retrieving-data-as-objects.md
For more info

Doesn't work with AngularFireStore, any solutions to this?

Was this page helpful?
0 / 5 - 0 ratings