3

I'm trying to create a "blog" with angular and firebase. I'm doing fine for my first project with both Angular and Firebase.

I made the frontpage where all the entries are loaded and then added a "read more..." hyperlink to the route of the specific article plus the id of the entry. Here's the HTML:

<div style="background-color:white" class=" contenedorBlog">

    <h1 style="padding-top:100px;" class="text-center mb-4">Pizarrón</h1>

    <div *ngFor="let item of items" style="padding:40px" class="rounded">
      <h2> {{item.title}} </h2>
      <p>Date: {{ item.systemDate }}</p>
      <div class="content" [innerHTML]="item.content"></div>
      <a [routerLink]="['/article', item.id]">Read more...</a>
    </div>

  </div>

And here's the HTML of the article component:

<div style="background-color:white" class=" contenedorBlog">

    <div style="padding:40px" class="rounded">
      <h2> {{items.title}} </h2>
      <p>Fecha: {{ items.systemDate }}</p>
      <div class="content" [innerHTML]="items.content"></div>
    </div>

  </div>

I'm trying to figure out how can i call the specific object having the id on the route (article/:id).

this is what i have so far (article.component.ts)

export class ArticlesComponent implements OnInit {

   items:any;

   constructor(private blogService: UploadService, private route: ActivatedRoute) {
      this.route.params.subscribe(params=> {
      this.items=this.blogService.getObjectById(params['id']);
   });
   }

   ngOnInit() {
   }

}

Problem is... i don't know what to do next. I'm calling a function insde a service that i want to retrieve me ONLY the object that contains the id that's on the route.

getObjectById(id) {
    ///Help
}

Thank you very much and sorry for my not that great english.

2 Answers 2

3

Change:

this.items=this.blogService.getObjectById(params['id']);

to:

this.blogService.getObjectById(params['id']).subscribe( i => {
        this.items = i;
    })

Them in getObjectById(id) :

getObjectById(id) { 
     return this.afs.collection('collectionName').doc(id).valueChanges()
}

If you want just one time the value use ".take(1)" before the ".subscribe"

Also you need to put *ngIf="items" in the element where you display "items" if you don't want see the "undefined" error.

Sign up to request clarification or add additional context in comments.

Comments

2

Define a AngularFirestore variable on your constructor, then use it in your function like this:

constructor(private afs: AngularFirestore){
  this.route.params.subscribe(params=> {
    this.getObjectById(params['id']);
  });
}

getObjectById(id) {
    return this.afs.collection('collection_name')
      .doc(id)
      .ref;
}

And then you might use the function on your component like:

this.blogService.getObjectById(params['id']).get().then(res => {
    console.log(res)
})

3 Comments

Hey, thank you very much... But it looks like it's not doing anything in my code, not even an error.
When i console log it, it prints an object like this: Object { _key: {…}, firestore: {…}, _firestoreClient: {…} } articles.component.ts:18:6
@Tagz The function there is returning just a reference to the object. I've edited my answer for how to access the document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.