1

I am new to firebase and angular, in step trying to learn I created data in firebase and now want to get in angular 5. But it's not working.

Firebase data structure: Firebase data structure:

category.service.ts:

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

@Injectable()
export class CategoryService {

  constructor(private db: AngularFireDatabase) { }

  getCategories(){
    return this.db.list('/categories').valueChanges();
  }

}

Product-form.component.ts

import { CategoryService } from './../../category.service';
import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';

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

  constructor(private categoryService: CategoryService) {
  }

  ngOnInit() {
    this.categories$ = this.categoryService.getCategories();
    console.log(this.categories$);
  }

}

product-form.component.html

<select class="form-control" name="" id="category">
        <option value=""></option>
        <option  *ngFor="let c of categories$ | async" [value]="c.$key">
          {{c.name}}
        </option>
    </select>

Error in console i am getting:

enter image description here

Please tell me what's wrong with my code.

4
  • 1
    Can you add the entire error log? Commented May 28, 2018 at 7:41
  • What does console.log(this.categories$)say? Commented May 28, 2018 at 7:46
  • Observable {_isScalar: false, source: Observable, operator: MapOperator} operator : MapOperator project : ƒ (actions) thisArg : undefined proto : Object source : Observable _isScalar : false _subscribe : ƒ (subscriber) proto : Object _isScalar : false proto : Object Commented May 28, 2018 at 8:52
  • hi did u got the solution of your problem.. Commented Aug 17, 2018 at 5:58

2 Answers 2

1

Hey are you using rxjs? Maybe consider looking in this thread where I already answered a similar issue. RXJS-version problem

It is in most cases the problem, that the rxjs version of yours is not on version 6. Also please consider if you are using a version less of 6 and you try to upgrade look into my answer. There is a migration guide you should follow for migrating to version 6.

Hope this will fix your Problem.

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

4 Comments

Yes i am usinig rxjs 5.5.6 and angular 5. To upgrade rxjs i have to update angular 5 that i don't want.
which angular fire version you are using ? the version from 5. is requiring rxjs 6.
Thanks @Benjamin....Its working now..instead upgrade the rxjs, i down grade the angularfire.
yeah thats a possibility but please consider that many of the angularfire2 features as the credentialLess authentication etc are part of high version. If this is no problem for you, stay it this way. Otherwise maybe you need to consider upgrading instead of downgrading.
0

You should try using the for in loop. Subscribe to the http observable and then populate it in an array or a data structure necessary for your business logic using the for in loop.

Then use this new data structure in the HTML. If you choose an array a for of loop would definitely work in your HTML file.

Hope that helps. Good Luck!!

Comments

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.