Retrieve single object Firebase Angular












0















I'm working on this application but I'm having problems retrieving the object's entire information.



I have a "news" page that will display all the lists from the DB, that one is working fine, when you click on the news, it will take you to the "details" page, I can do the routing, no problem.



The issue is that I can't get the data from Firebase. What am I doing wrong?



Thanks



Component:



import { ActivatedRoute, Params, Router } from '@angular/router';
import { AngularFireDatabase, AngularFireObject } from '@angular/fire/database';
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { NewsService } from '../news.service';
import { Observable } from 'rxjs';

@Component({
selector: 'app-display',
templateUrl: './display.component.html',
styleUrls: ['./display.component.css']
})
export class NewsDisplayComponent implements OnInit {
@ViewChild('closeBtn') closeBtn: ElementRef;
id;

news: Observable<any>;

constructor(private db: AngularFireDatabase, private route: ActivatedRoute, private newsService: NewsService, private router: Router) { }

ngOnInit() {
this.id = this.route.snapshot.params['id'];
this.news = this.newsService.getSingleNews(this.id).valueChanges();
console.log(this.news);
}

closeModal() {
this.router.navigate(['/news']);
this.closeBtn.nativeElement.click();
}

}


Service.ts



import { map } from 'rxjs/operators';
import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
import { News } from './news.model';
import { Injectable } from '@angular/core';

@Injectable()
export class NewsService {
news: AngularFireList<News>;
constructor(private db: AngularFireDatabase) {
}
getNews() {
this.news = this.db.list('news');
return this.news.valueChanges();
}

getSingleNews(id: string) {
return this.db.object('news' + id);
}
}


HTML



<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">{{ id }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" #closeBtn (click)="closeModal()">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<h1>{{ news.title | async }}</h1>
<h2>{{ news.subtitle | async }}</h2>
<p>{{ news.article | async}}</p>
</div>
</div>
</div>









share|improve this question



























    0















    I'm working on this application but I'm having problems retrieving the object's entire information.



    I have a "news" page that will display all the lists from the DB, that one is working fine, when you click on the news, it will take you to the "details" page, I can do the routing, no problem.



    The issue is that I can't get the data from Firebase. What am I doing wrong?



    Thanks



    Component:



    import { ActivatedRoute, Params, Router } from '@angular/router';
    import { AngularFireDatabase, AngularFireObject } from '@angular/fire/database';
    import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
    import { NewsService } from '../news.service';
    import { Observable } from 'rxjs';

    @Component({
    selector: 'app-display',
    templateUrl: './display.component.html',
    styleUrls: ['./display.component.css']
    })
    export class NewsDisplayComponent implements OnInit {
    @ViewChild('closeBtn') closeBtn: ElementRef;
    id;

    news: Observable<any>;

    constructor(private db: AngularFireDatabase, private route: ActivatedRoute, private newsService: NewsService, private router: Router) { }

    ngOnInit() {
    this.id = this.route.snapshot.params['id'];
    this.news = this.newsService.getSingleNews(this.id).valueChanges();
    console.log(this.news);
    }

    closeModal() {
    this.router.navigate(['/news']);
    this.closeBtn.nativeElement.click();
    }

    }


    Service.ts



    import { map } from 'rxjs/operators';
    import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
    import { News } from './news.model';
    import { Injectable } from '@angular/core';

    @Injectable()
    export class NewsService {
    news: AngularFireList<News>;
    constructor(private db: AngularFireDatabase) {
    }
    getNews() {
    this.news = this.db.list('news');
    return this.news.valueChanges();
    }

    getSingleNews(id: string) {
    return this.db.object('news' + id);
    }
    }


    HTML



    <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
    <div class="modal-header">
    <h5 class="modal-title" id="exampleModalCenterTitle">{{ id }}</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close" #closeBtn (click)="closeModal()">
    <span aria-hidden="true">&times;</span>
    </button>
    </div>
    <div class="modal-body">
    <h1>{{ news.title | async }}</h1>
    <h2>{{ news.subtitle | async }}</h2>
    <p>{{ news.article | async}}</p>
    </div>
    </div>
    </div>









    share|improve this question

























      0












      0








      0








      I'm working on this application but I'm having problems retrieving the object's entire information.



      I have a "news" page that will display all the lists from the DB, that one is working fine, when you click on the news, it will take you to the "details" page, I can do the routing, no problem.



      The issue is that I can't get the data from Firebase. What am I doing wrong?



      Thanks



      Component:



      import { ActivatedRoute, Params, Router } from '@angular/router';
      import { AngularFireDatabase, AngularFireObject } from '@angular/fire/database';
      import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
      import { NewsService } from '../news.service';
      import { Observable } from 'rxjs';

      @Component({
      selector: 'app-display',
      templateUrl: './display.component.html',
      styleUrls: ['./display.component.css']
      })
      export class NewsDisplayComponent implements OnInit {
      @ViewChild('closeBtn') closeBtn: ElementRef;
      id;

      news: Observable<any>;

      constructor(private db: AngularFireDatabase, private route: ActivatedRoute, private newsService: NewsService, private router: Router) { }

      ngOnInit() {
      this.id = this.route.snapshot.params['id'];
      this.news = this.newsService.getSingleNews(this.id).valueChanges();
      console.log(this.news);
      }

      closeModal() {
      this.router.navigate(['/news']);
      this.closeBtn.nativeElement.click();
      }

      }


      Service.ts



      import { map } from 'rxjs/operators';
      import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
      import { News } from './news.model';
      import { Injectable } from '@angular/core';

      @Injectable()
      export class NewsService {
      news: AngularFireList<News>;
      constructor(private db: AngularFireDatabase) {
      }
      getNews() {
      this.news = this.db.list('news');
      return this.news.valueChanges();
      }

      getSingleNews(id: string) {
      return this.db.object('news' + id);
      }
      }


      HTML



      <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
      <div class="modal-header">
      <h5 class="modal-title" id="exampleModalCenterTitle">{{ id }}</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close" #closeBtn (click)="closeModal()">
      <span aria-hidden="true">&times;</span>
      </button>
      </div>
      <div class="modal-body">
      <h1>{{ news.title | async }}</h1>
      <h2>{{ news.subtitle | async }}</h2>
      <p>{{ news.article | async}}</p>
      </div>
      </div>
      </div>









      share|improve this question














      I'm working on this application but I'm having problems retrieving the object's entire information.



      I have a "news" page that will display all the lists from the DB, that one is working fine, when you click on the news, it will take you to the "details" page, I can do the routing, no problem.



      The issue is that I can't get the data from Firebase. What am I doing wrong?



      Thanks



      Component:



      import { ActivatedRoute, Params, Router } from '@angular/router';
      import { AngularFireDatabase, AngularFireObject } from '@angular/fire/database';
      import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
      import { NewsService } from '../news.service';
      import { Observable } from 'rxjs';

      @Component({
      selector: 'app-display',
      templateUrl: './display.component.html',
      styleUrls: ['./display.component.css']
      })
      export class NewsDisplayComponent implements OnInit {
      @ViewChild('closeBtn') closeBtn: ElementRef;
      id;

      news: Observable<any>;

      constructor(private db: AngularFireDatabase, private route: ActivatedRoute, private newsService: NewsService, private router: Router) { }

      ngOnInit() {
      this.id = this.route.snapshot.params['id'];
      this.news = this.newsService.getSingleNews(this.id).valueChanges();
      console.log(this.news);
      }

      closeModal() {
      this.router.navigate(['/news']);
      this.closeBtn.nativeElement.click();
      }

      }


      Service.ts



      import { map } from 'rxjs/operators';
      import { AngularFireDatabase, AngularFireList } from '@angular/fire/database';
      import { News } from './news.model';
      import { Injectable } from '@angular/core';

      @Injectable()
      export class NewsService {
      news: AngularFireList<News>;
      constructor(private db: AngularFireDatabase) {
      }
      getNews() {
      this.news = this.db.list('news');
      return this.news.valueChanges();
      }

      getSingleNews(id: string) {
      return this.db.object('news' + id);
      }
      }


      HTML



      <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
      <div class="modal-header">
      <h5 class="modal-title" id="exampleModalCenterTitle">{{ id }}</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close" #closeBtn (click)="closeModal()">
      <span aria-hidden="true">&times;</span>
      </button>
      </div>
      <div class="modal-body">
      <h1>{{ news.title | async }}</h1>
      <h2>{{ news.subtitle | async }}</h2>
      <p>{{ news.article | async}}</p>
      </div>
      </div>
      </div>






      angular firebase angularfire2 angularfire






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 23:17









      iconioiconio

      789




      789
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Try it like this:



          Component



          this.newsService.getSingleNews(this.id).subscribe(news => {
          this.news = news
          });


          Service.ts



          It seems like you're missing a slash after news.



          getSingleNews(id: string) {
          return this.db.object('news/' + id);
          }


          Update:



          Change news: Observable<any> property to an object. If you do it the way I showed you it isn't an observable anymore.






          share|improve this answer


























          • Hey ams. Thanks! I didn't work. I got the samething. Nothing shows. :(

            – iconio
            Nov 16 '18 at 22:02











          • Have you changed the news: Observable<any> property to an object? because if you do it the way I showed you it isn't an observable anymore. Also what does the data news from the subscription show when you console.log it.

            – ams
            Nov 17 '18 at 1:19











          • ams that worked! :) Thank you sooo much!

            – iconio
            Nov 18 '18 at 13:23











          • @iconio Nice to hear! Could you mark my answer as the correct one, so that other people in the future with the same problem will see that this solution works?

            – ams
            Nov 18 '18 at 21:50











          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%2f53329213%2fretrieve-single-object-firebase-angular%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Try it like this:



          Component



          this.newsService.getSingleNews(this.id).subscribe(news => {
          this.news = news
          });


          Service.ts



          It seems like you're missing a slash after news.



          getSingleNews(id: string) {
          return this.db.object('news/' + id);
          }


          Update:



          Change news: Observable<any> property to an object. If you do it the way I showed you it isn't an observable anymore.






          share|improve this answer


























          • Hey ams. Thanks! I didn't work. I got the samething. Nothing shows. :(

            – iconio
            Nov 16 '18 at 22:02











          • Have you changed the news: Observable<any> property to an object? because if you do it the way I showed you it isn't an observable anymore. Also what does the data news from the subscription show when you console.log it.

            – ams
            Nov 17 '18 at 1:19











          • ams that worked! :) Thank you sooo much!

            – iconio
            Nov 18 '18 at 13:23











          • @iconio Nice to hear! Could you mark my answer as the correct one, so that other people in the future with the same problem will see that this solution works?

            – ams
            Nov 18 '18 at 21:50
















          1














          Try it like this:



          Component



          this.newsService.getSingleNews(this.id).subscribe(news => {
          this.news = news
          });


          Service.ts



          It seems like you're missing a slash after news.



          getSingleNews(id: string) {
          return this.db.object('news/' + id);
          }


          Update:



          Change news: Observable<any> property to an object. If you do it the way I showed you it isn't an observable anymore.






          share|improve this answer


























          • Hey ams. Thanks! I didn't work. I got the samething. Nothing shows. :(

            – iconio
            Nov 16 '18 at 22:02











          • Have you changed the news: Observable<any> property to an object? because if you do it the way I showed you it isn't an observable anymore. Also what does the data news from the subscription show when you console.log it.

            – ams
            Nov 17 '18 at 1:19











          • ams that worked! :) Thank you sooo much!

            – iconio
            Nov 18 '18 at 13:23











          • @iconio Nice to hear! Could you mark my answer as the correct one, so that other people in the future with the same problem will see that this solution works?

            – ams
            Nov 18 '18 at 21:50














          1












          1








          1







          Try it like this:



          Component



          this.newsService.getSingleNews(this.id).subscribe(news => {
          this.news = news
          });


          Service.ts



          It seems like you're missing a slash after news.



          getSingleNews(id: string) {
          return this.db.object('news/' + id);
          }


          Update:



          Change news: Observable<any> property to an object. If you do it the way I showed you it isn't an observable anymore.






          share|improve this answer















          Try it like this:



          Component



          this.newsService.getSingleNews(this.id).subscribe(news => {
          this.news = news
          });


          Service.ts



          It seems like you're missing a slash after news.



          getSingleNews(id: string) {
          return this.db.object('news/' + id);
          }


          Update:



          Change news: Observable<any> property to an object. If you do it the way I showed you it isn't an observable anymore.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 18 '18 at 21:49

























          answered Nov 15 '18 at 23:45









          amsams

          23818




          23818













          • Hey ams. Thanks! I didn't work. I got the samething. Nothing shows. :(

            – iconio
            Nov 16 '18 at 22:02











          • Have you changed the news: Observable<any> property to an object? because if you do it the way I showed you it isn't an observable anymore. Also what does the data news from the subscription show when you console.log it.

            – ams
            Nov 17 '18 at 1:19











          • ams that worked! :) Thank you sooo much!

            – iconio
            Nov 18 '18 at 13:23











          • @iconio Nice to hear! Could you mark my answer as the correct one, so that other people in the future with the same problem will see that this solution works?

            – ams
            Nov 18 '18 at 21:50



















          • Hey ams. Thanks! I didn't work. I got the samething. Nothing shows. :(

            – iconio
            Nov 16 '18 at 22:02











          • Have you changed the news: Observable<any> property to an object? because if you do it the way I showed you it isn't an observable anymore. Also what does the data news from the subscription show when you console.log it.

            – ams
            Nov 17 '18 at 1:19











          • ams that worked! :) Thank you sooo much!

            – iconio
            Nov 18 '18 at 13:23











          • @iconio Nice to hear! Could you mark my answer as the correct one, so that other people in the future with the same problem will see that this solution works?

            – ams
            Nov 18 '18 at 21:50

















          Hey ams. Thanks! I didn't work. I got the samething. Nothing shows. :(

          – iconio
          Nov 16 '18 at 22:02





          Hey ams. Thanks! I didn't work. I got the samething. Nothing shows. :(

          – iconio
          Nov 16 '18 at 22:02













          Have you changed the news: Observable<any> property to an object? because if you do it the way I showed you it isn't an observable anymore. Also what does the data news from the subscription show when you console.log it.

          – ams
          Nov 17 '18 at 1:19





          Have you changed the news: Observable<any> property to an object? because if you do it the way I showed you it isn't an observable anymore. Also what does the data news from the subscription show when you console.log it.

          – ams
          Nov 17 '18 at 1:19













          ams that worked! :) Thank you sooo much!

          – iconio
          Nov 18 '18 at 13:23





          ams that worked! :) Thank you sooo much!

          – iconio
          Nov 18 '18 at 13:23













          @iconio Nice to hear! Could you mark my answer as the correct one, so that other people in the future with the same problem will see that this solution works?

          – ams
          Nov 18 '18 at 21:50





          @iconio Nice to hear! Could you mark my answer as the correct one, so that other people in the future with the same problem will see that this solution works?

          – ams
          Nov 18 '18 at 21:50




















          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%2f53329213%2fretrieve-single-object-firebase-angular%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

          The Sandy Post

          Danny Elfman

          Pages that link to "Head v. Amoskeag Manufacturing Co."