Access specific item within a Firebase Collection using angular

Asked

Viewed 221 times

-2

I’m with an angular project and I’m using firebase cloud firestore as a database but I’m having a hard time using it because I have a collection called feed and within this collection I have several items and I need to pick only one item using its key however all the methods I tried didn’t work.

my service:

import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';


@Injectable()

export class TestService {
  constructor(private firestore: AngularFirestore) { }
  /* create_NewIcecream : Cria um novo registro na coleção especificada usando o método add */
  criar_novo_user(record) {
    return this.firestore.collection('user').add(record);
  }
  /*read_Icecream: Chama o método snapshotChanges , que obterá registros e também será registrado para receber atualizações */
  read_userPrincipal(){
    return this.firestore.collection('user').snapshotChanges();
  }
  read_feddUser(key:string){
    return this.firestore.collection('feed').get;
  }
  update_like(recordID,record) {
   return this.firestore.doc('feed/' + recordID).update(record);
  }
  carregar_publicacao(){
    return this.firestore.collection('feed').snapshotChanges();
  }

}

my component

import { Component, OnInit, Input } from '@angular/core';
import { Publicacao } from './publi.model';
import { TestService } from 'src/app/test.service';


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

  @Input() publicacao: Publicacao

  constructor(private testeService: TestService) { }


  ngOnInit(){}

  like(item){
    let arrey;
    arrey = this.testeService.read_feddUser(item.token)
    console.log(arrey)
  }
}

in that like function is where I need to read only the item with its key.

1 answer

0

It is possible to check some problems in the method read_feddUser.

get is a method. Therefore, it is necessary to change to get().

Although receive a parameter (key:string), it is not used. In this case you would not be able to return an item by its ID. It is necessary to change to

this.firestore.collection('feed').doc(key).get();

You return the get() from the firestore to your component and get() returns a promisse. So you need to change your component to read promisse. Something like this:

like(item) {
  let feed;
  this.testeService.read_feddUser(item.token)
  .then(function(doc) {
    feed = doc;
    console.log(feed);
  }
  • i put the function as you showed there and it from the compilation error

  • read_feddUser(key:string){ Return firebase.firestore(). Collection('feed'). doc(key). get(); }

  • import * as firebase from firebase;?

  • ERROR in . /firebase 37:8 Module parse failed: Unexpected token (37:8) You may need an appropriate Loader to Handle this file type, Currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders | }) | console.log(this.publicacoes) > } | |

  • in your case should be this.firestore.collection('feed').doc(key).get(); corrected in response.

  • worked @jvl?

  • thus putting the error actually some of the service but error in the component in the function then

  • https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document

  • @jvl check how it is in the documentation, this link above

  • but doing like this error also because it says that get is not a function and then is also not a function.

  • Return this.firestore.Collection('feed'). doc(key). get(); if I leave my service function so and the Component function so Let feed ; feed = this.testeService.read_feddUser(item.token) console.log(feed) it returns me an observable as I manage to access the properties in that observable in the course I did I understood almost nothing of Observable

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.