【问题标题】:I can't display my single product from firebase with angular我无法使用 angular 显示来自 firebase 的单个产品
【发布时间】:2020-11-22 18:43:59
【问题描述】:

所以我试图在产品详细信息页面中显示我的产品,但我什么也没显示。 我不知道问题出在哪里。我真的需要一些帮助,因为如果这个问题没有得到解决,我将无法更进一步。谢谢大家。

product.service.ts
____________________

// Declarations
  bayonete: Observable<CutiteInterface[]>;
  loadBayonete: AngularFirestoreCollection<CutiteInterface>;
  bayonetProd;

// get all products
 GetBayonete() {
    this.loadBayonete = this.afStore.collection("bayonete");
    this.bayonetProd = this.loadBayonete.snapshotChanges().pipe(
      map((action) => {
        return action.map((a) => {
          const data = a.payload.doc.data() as CutiteInterface;
          const id = a.payload.doc.id;        
          return { id, ...data };
        });
      })
    );
    return this.bayonetProd;
  }


// get single product
  getProductDetails(id) {
    return this.afStore.doc<CutiteInterface>("bayonete/" + id);
  }

现在在我的详细信息页面中,我调用了我的产品,我可以在控制台中看到数组,但我无法显示它

detail-product.page.ts
_____________________

// Declarations
  product?: CutiteInterface;
  bayonetSingle: Observable<CutiteInterface[]>;
  bayonetID: string;

 constructor(
    private route: ActivatedRoute,
    private navCtrl: NavController,
    private ProduseService: ProduseService,
    private afStore: AngularFirestore
  ) {

    this.bayonetID = this.route.snapshot.params["id"];
    this.ProduseService.GetBayonete().subscribe((products) => {
      this.product = products.find((a) => a.id === this.bayonetID);
      console.log(this.product);
    });
   
    this.bayonetSingle = this.ProduseService.getProductDetails(
      this.bayonetID
    ).valueChanges();
    console.log(this.bayonetSingle);
  }

这里我需要显示它:

product-detail.html

 <ion-grid>
      <ion-row>
        <ion-col size="12">
          <ion-list>
            <div class="product_descriere">
              {{ (bayonetSingle | async)?.description }}
            </div>
            <div class="product_reducere">
              {{ (bayonetSingle | async)?.reducere }} Lei ( {{ (bayonetSingle | async)?.tag }} )
            </div>
            <div class="product_pret">
              {{ (bayonetSingle | async)?.price }} Lei
            </div>
          </ion-list>
        </ion-col>
      </ion-row>
    </ion-grid>

【问题讨论】:

  • 这看起来不像 Firebase 的问题。首先,请不要告诉我们没有发生什么,告诉我们您希望看到什么以及您得到的实际输出是什么。例如,那些 console.log 行打印出来的是什么?其次,我可以看到您正在将信息检索到“产品”中,但您没有使用它来打印信息,您能否解释一下这是如何工作的?

标签: angular typescript firebase google-cloud-firestore angularfire2


【解决方案1】:

尝试这样的单品更换方法:

getProductDetails(id) {
    return this.afStore.doc<CutiteInterface>("bayonete/" + id).get();
  }

并在您对此进行详细的页面更改

this.bayonetSingle = this.ProduseService.getProductDetails(this.bayonetID)
.subscribe(proddet => {
    this.bayonetSingle = proddet.data();
    console.log(this.bayonetSingle);
    });
    

如果这不起作用,请尝试在 You detal page creat all 方法中,例如。

const dbRef = this.afStore.doc<CutiteInterface>("bayonete/" + id);
dbRef.get().subscribe(prdet => {
const data = prdet.data();
console.log(data);
});

好的新版本我们尝试重写一些方法

// get all products
 GetBayonete() {
    this.loadBayonete = this.afStore.collection("bayonete");
   return this.loadBayonete.snapshotChanges().pipe(
      map(action => action.map(a => {
      const data = a.payload.doc.data() as CutiteInterface;
      const id = a.payload.doc.id;        
      return { id, ...data };
    }))
    );
   }
  }

对于单品

// get single product
  getProductDetails(id) {
    return this.afStore.collection("bayonete").doc(id).snapshotChanges();
  }

在你的组件页面中是这样的。

ngOnInit():void {
this.ProduseService.getProductDetails(this.bayonetID).subscribe(px => {
const data = px.payload.data() as CutiteInterface;;
this.beyondSingle = data;
// for test purpose
console.log(bayonetSingle.description);
}

在你的html页面中改变这个

<div class="product_descriere">
          {{ (bayonetSingle | async)?.description }}
        </div>

为此

<div class="product_descriere">
              {{ bayonetSingle.description }}
            </div>

你试试这个

<div class="product_descriere">
              {{ bayonetSingle.description | async }}
            </div>

如果您有任何问题,请不要犹豫

【讨论】:

  • 您好,感谢您的帮助!我试过你的方法,但我得到错误:“错误错误:InvalidPipeArgument:'[object Object]' for pipe'AsyncPipe'”和console.log让我'未定义'。问题出在html页面上,我猜异步管道不起作用。但是函数 this.bayonetSingle = this.ProduseService.GetBayonete().subscribe( (products) => { this.product_single = products.find((a) => a.id === this.bayonetID); 返回控制台。日志(this.product);});给我整个文档,所以我只需要显示它.. idk 如何
  • 这很奇怪,因为今天在我的项目上工作时,在某个时候服务下载数据停止工作,将方法直接放入组件中工作正常。这是非常神秘的:)
  • 您能帮我解决这个问题吗?我的意思是,如果您有任何解决方案.. 它将是完美的
  • link - 这是安慰该方法的屏幕截图,但我无法找到解决方案来展示它xd omg它让我发疯
  • 我把我所有的代码都改成了这个新方法,但是这里是在测试 console.log(bayonetSingle.description);它找不到我尝试使用的这个变量,但随后说“Observable 类型上不存在描述......我试图控制台记录数据,但我得到未定义。??
猜你喜欢
  • 2017-07-13
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 2017-11-20
  • 2017-08-17
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多