0

I have a service defined with these imports:

import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
import "rxjs/add/operator/map";
import { Subject } from "rxjs/Subject";
import { Navigation } from "../Shared/app.home";
import { Module } from "../Shared/app.home";

Then in the service I define a method:

getNavigationModules(): Observable<Navigation[]> {
    return this.http.get<Module[]>("/api/modules/getuserpreferences").map((mods) => {
        return mods.map(m => {
            var nav = new Navigation();
            nav.id = m.Id;
            nav.name = m.Name;
            nav.isFavorite = m.IsFavorite;
            nav.url = m.FriendlyUrl;
            nav.status = (m.ModuleProperties["Status"] != null && m.ModuleProperties["Status"] === "Online") ? "on" : "off" ;
            nav.type = m.Type;

            return nav;
        });
    });
}

When the method is called, I get the error that map is undefined. When calling mods.map I assume that by calling http.get that mods will be an Module array, but when I use the debugger (Chrome) I see that the debugger treats mods as an Object with a property Modules, this one being the Module array.

enter image description here

But if I change the code to mods.Modules.map I get an error when compiling because mods is in fact an Module[] and not an object.

What am I missing?

Thanks

4
  • unless you subscribe it will be undefined Commented Jan 29, 2018 at 10:54
  • But I want to return an Observable, and subscribe returns a Subscription. Commented Jan 29, 2018 at 11:03
  • Can you reproduce using a plunker or stackblitz Commented Jan 29, 2018 at 11:07
  • I was doing it, but @JB Nizet posted a solution that works. Thx Commented Jan 29, 2018 at 11:33

1 Answer 1

2

You're telling TypeScript that what the backend will send back is an array of Module. But it's not. It's an object, with one field named Modules, which is an array of Module. So tell the correct thing to TypeScript:

getNavigationModules(): Observable<Navigation[]> {
    return this.http.get<{Modules: Module[];}>("/api/modules/getuserpreferences").map((mods) => {
        return mods.Modules.map(m => {
            var nav = new Navigation();
            nav.id = m.Id;
            nav.name = m.Name;
            nav.isFavorite = m.IsFavorite;
            nav.url = m.FriendlyUrl;
            nav.status = (m.ModuleProperties["Status"] != null && m.ModuleProperties["Status"] === "Online") ? "on" : "off" ;
            nav.type = m.Type;

            return nav;
        });
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's it :) Thx!

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.