gpt4 book ai didi

html - 数据未从 Angular 7 中的 Firebase 检索

转载 作者:太空狗 更新时间:2023-10-29 18:27:54 26 4
gpt4 key购买 nike

我想从 firebase 数据库中检索数据集并填充我的下拉列表。但是什么也没有出现。我做了很多研究,但我仍然无法解决这个问题。我正在使用 Angular 7

ERROR Error: "Cannot find a differ supporting object 'function (events) {
var snapshotChanges$ = Object(_snapshot_changes__WEBPACK_IMPORTED_MODULE_0__["snapshotChanges"])(query, events);
return afDatabase.scheduler.keepUnstableUntilFirst(afDatabase.scheduler.runOutsideAngular(snapshotChanges$)).pipe(Object(rxjs_operators__WEBPACK_IMPORTED_MODULE_5__["map"])(function (actions) { return actions.map(function (a) { return a.payload.val(); }); }));
}' of type 'valueChanges'. NgFor only supports binding to Iterables such as Arrays."

category.service.ts

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

@Injectable()
export class CategoryService {

constructor(private db: AngularFireDatabase) { }

getCategories(){

return this.db.list('/categories').valueChanges;//get categories list in database and return it
}
}

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 } from '@angular/router';

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

categories$;

constructor(private router: Router, private categoryService: CategoryService, private productService: ProductService) {
this.categories$ = categoryService.getCategories();
}

product-from.component.html中与问题相关的代码段

 <div class="form-group">
<label for = "category">category</label>

<select #category="ngModel" ngModel name="category" id ="category"class="form-control" required>
<option value=""></option>
<option *ngFor="let cate of categories$" [value]=" cate.$key ">{{ cate.name }}</option>
</select>

<div class="alert alert-danger" *ngIf="category.touched && category.invalid">Category is required.
</div>
</div>

这是我的数据库的样子

This is how my categories entry looks like

最佳答案

This is how my database looks like

问题很可能出在您的数据库设计上。尝试将其从基于对象的设计更改为数组设计,如下所示。

导出来自 firebase 的 JSON 文件并更改类别为 array .所以它将如下所示。

"Categories": [
{ "name":"Bread"},
{ "name":"Dairy" },
{ "name":"Fruits" },
.....
.....
.....
]

将修改后的 JSON 导入到 firebase。

关于html - 数据未从 Angular 7 中的 Firebase 检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55411825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com