Angular + Firebase:TypeError:this.productService.get(...)。take不是一个函数

时间:2019-02-09 06:15:13

标签: angular firebase

我正在尝试使用键从Firebase检索单个对象。我不想为.subscribe()方法实现OnDestroy,而是想使用take()属性来获取单个对象并完成它。如何解决这个问题呢?请指引我正确的方向

我因TypeError出现错误:this.productService.get(...)。take不是函数TypeError:this.productService.get(...)。take不是函数。

product-form.component.ts

import { Component, OnInit } from '@angular/core';
import { CategoryService } from 'src/app/category.service';
import { ProductService } from 'src/app/product.service';
import { Router, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/take';

@Component({
  selector: 'app-product-form',
  templateUrl: './product-form.component.html',
  styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
  categories$;
  product = {};
  
  
  constructor(
    private router:Router,
    private route: ActivatedRoute,
    private categoryService: CategoryService, 
    private productService: ProductService) { 
    this.categories$ = categoryService.getcategories();
   
    let id = this.route.snapshot.paramMap.get('id');
    if(id) {
      this.productService
        .get(id)
        .take(1)
        .subscribe(p => this.product = p)
    }
  }
   
  save(product){
    this.productService.create(product);
    this.router.navigate(['/admin/products']);
  }
  
  ngOnInit() {
  }

}
product.service.ts

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';

@Injectable({
  providedIn: 'root'
})
export class ProductService {

  constructor(private db: AngularFireDatabase) { }
 
  create(product) {
    return this.db.list('/products').push(product);
   }

   getAll() {
      return this.db.list('/products');
   }

   get(productId){
     return this.db.object('/products/' +productId);
   } 
}

1 个答案:

答案 0 :(得分:1)

您没有读取数据请尝试

服务中:

Helper helper=new Helper();
helper.showEntity(getNewMessage().get());

Supplier<Message> getNewMessage(){
   return Message::new;
}

在组件中:

get(productId){
   return this.db.object('/products/' +productId).valueChanges();
}