0

Angular 2 is fairly new. I have been using firebase-angular2 to create a service. I ran the firebase-angular2 demo here https://github.com/KallynGowdy/firebase-angular2-demo but when I have been running it I get the following error

EXCEPTION: TypeError: heroes.map is not a function angular2.dev.js:23083 EXCEPTION: TypeError: heroes.map is not a function

As far as I can tell it is referring to this section of code at ts/firebase-heros.service.ts

import {Injectable} from "../../node_modules/angular2/core";
import {HeroService} from "./hero.service";
import {Observable} from "../../node_modules/rxjs/Rx";
import {FirebaseService} from '../../node_modules/firebase-angular2/core';
import {Hero} from "./../interfaces/hero";

@Injectable()
export class FirebaseHeroService extends HeroService {

private service:FirebaseService;

constructor(firebaseService:FirebaseService) {
    this.service = firebaseService.child('heroes');
}

getHeroes() {
    var service = this.service;
    return service.value.map((heroes) => {
        return heroes.map((h, i) => {
            // TODO: Cleanup
            return {
                id: h.id,
                name: h.name,
                save: function () {
                    return service.child(i.toString()).setData({
                        id: this.id,
                        name: this.name
                    });
                }
            }
        })
    });
}
}

Any ideas of what may be causing this problem? thanks in advance

1 Answer 1

2

heroes is not array at this line, that's why you're getting an error:

return service.value.map((heroes) => {
  console.log(heroes);
  ...

If you log it's value you can check what type it is and take appropriate action. If it's a response, you might need to convert it to JSON before processing it further

return service.value
 .map(response => response.json())
 .map((heroes) => {...
Sign up to request clarification or add additional context in comments.

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.