Angular 4 Return http response as interface with JSON as property












0















I had a previously working service that used to only return the JSON response into my component (see second section for previously working code). However, I am building in additional functionality into all of my components to have loading animations and error messages so I need the response.OK and response.status code as well. I created this generic response interface to use in a global variable service I have as follows:



export interface GetResponse  {
status: number,
ok: boolean,
json: any
}


I am using it in my service as follows:



   getInfoDeskTopic(topicUrl) {
this.infoDeskData = ;
this.http.get(topicUrl, this.options)
.map(res => this.response = res)
.map(res => this.response.json = res.json().resultSet.results)
this.response.json
.map(data => data
.forEach(item => {
let infoDesk = <InfoDesk>{};
infoDesk.Url = item.json.webUrl;
infoDesk.Date = item.date;
item.fields.forEach(item => {
if (item.id == 'publication') {
infoDesk.Publication = item.strings[0];
}
if (item.id == 'headline') {
infoDesk.Title = item.strings[0];
}
});
this.infoDeskData.push(infoDesk);
}))
.map(data => data = this.infoDeskData)
.map(items => items.slice(0, 5))
.share();
return this.response;
}


And I am attempting to use it in my component as follows:



this.response = this.infodesk.getInfoDeskTopic(this.topicUrl);
this.news = this.response.json;
this.success = this.response.ok;


I have the variable in my component (this.response: any). I receive the error:



Uncaught Error: Can't resolve all parameters for InfoDeskService: ([object Object], ?).
at syntaxError (compiler.es5.js:1540)


Can any help me understand why I am getting an error? I feel like I am declaring something incorrectly perhaps, but I cannot figure out what the issue is. Thanks



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





The following is what I had PREVIOUSLY and it was working fine (just for reference):



In my service:



   getInfoDeskTopic(topicUrl) {
this.infoDeskData = ;
return this.http.get(topicUrl, this.options)
.map(res => res.json().resultSet.results)
.map(data => data
.forEach(item => {
let infoDesk = <InfoDesk>{};
infoDesk.Url = item.json.webUrl;
infoDesk.Date = item.date;
item.fields.forEach(item => {
if (item.id == 'publication') {
infoDesk.Publication = item.strings[0];
}
if (item.id == 'headline') {
infoDesk.Title = item.strings[0];
}
});
this.infoDeskData.push(infoDesk);
}))
.map(data => data = this.infoDeskData)
.map(items => items.slice(0, 5))
.share();
}


And in my component:



this.news = this.infodesk.getInfoDeskTopic(this.topicUrl);









share|improve this question























  • I have gotten rid of the error mentioned above by declaring the interface as the type for a variable in the service as opposed to in the constructor of the service. However, now I am getting the error: "Cannot read property 'ok' of undefined" from when I am trying to set the variable in my constructor

    – kittycatbytes
    Aug 1 '17 at 17:40
















0















I had a previously working service that used to only return the JSON response into my component (see second section for previously working code). However, I am building in additional functionality into all of my components to have loading animations and error messages so I need the response.OK and response.status code as well. I created this generic response interface to use in a global variable service I have as follows:



export interface GetResponse  {
status: number,
ok: boolean,
json: any
}


I am using it in my service as follows:



   getInfoDeskTopic(topicUrl) {
this.infoDeskData = ;
this.http.get(topicUrl, this.options)
.map(res => this.response = res)
.map(res => this.response.json = res.json().resultSet.results)
this.response.json
.map(data => data
.forEach(item => {
let infoDesk = <InfoDesk>{};
infoDesk.Url = item.json.webUrl;
infoDesk.Date = item.date;
item.fields.forEach(item => {
if (item.id == 'publication') {
infoDesk.Publication = item.strings[0];
}
if (item.id == 'headline') {
infoDesk.Title = item.strings[0];
}
});
this.infoDeskData.push(infoDesk);
}))
.map(data => data = this.infoDeskData)
.map(items => items.slice(0, 5))
.share();
return this.response;
}


And I am attempting to use it in my component as follows:



this.response = this.infodesk.getInfoDeskTopic(this.topicUrl);
this.news = this.response.json;
this.success = this.response.ok;


I have the variable in my component (this.response: any). I receive the error:



Uncaught Error: Can't resolve all parameters for InfoDeskService: ([object Object], ?).
at syntaxError (compiler.es5.js:1540)


Can any help me understand why I am getting an error? I feel like I am declaring something incorrectly perhaps, but I cannot figure out what the issue is. Thanks



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





The following is what I had PREVIOUSLY and it was working fine (just for reference):



In my service:



   getInfoDeskTopic(topicUrl) {
this.infoDeskData = ;
return this.http.get(topicUrl, this.options)
.map(res => res.json().resultSet.results)
.map(data => data
.forEach(item => {
let infoDesk = <InfoDesk>{};
infoDesk.Url = item.json.webUrl;
infoDesk.Date = item.date;
item.fields.forEach(item => {
if (item.id == 'publication') {
infoDesk.Publication = item.strings[0];
}
if (item.id == 'headline') {
infoDesk.Title = item.strings[0];
}
});
this.infoDeskData.push(infoDesk);
}))
.map(data => data = this.infoDeskData)
.map(items => items.slice(0, 5))
.share();
}


And in my component:



this.news = this.infodesk.getInfoDeskTopic(this.topicUrl);









share|improve this question























  • I have gotten rid of the error mentioned above by declaring the interface as the type for a variable in the service as opposed to in the constructor of the service. However, now I am getting the error: "Cannot read property 'ok' of undefined" from when I am trying to set the variable in my constructor

    – kittycatbytes
    Aug 1 '17 at 17:40














0












0








0








I had a previously working service that used to only return the JSON response into my component (see second section for previously working code). However, I am building in additional functionality into all of my components to have loading animations and error messages so I need the response.OK and response.status code as well. I created this generic response interface to use in a global variable service I have as follows:



export interface GetResponse  {
status: number,
ok: boolean,
json: any
}


I am using it in my service as follows:



   getInfoDeskTopic(topicUrl) {
this.infoDeskData = ;
this.http.get(topicUrl, this.options)
.map(res => this.response = res)
.map(res => this.response.json = res.json().resultSet.results)
this.response.json
.map(data => data
.forEach(item => {
let infoDesk = <InfoDesk>{};
infoDesk.Url = item.json.webUrl;
infoDesk.Date = item.date;
item.fields.forEach(item => {
if (item.id == 'publication') {
infoDesk.Publication = item.strings[0];
}
if (item.id == 'headline') {
infoDesk.Title = item.strings[0];
}
});
this.infoDeskData.push(infoDesk);
}))
.map(data => data = this.infoDeskData)
.map(items => items.slice(0, 5))
.share();
return this.response;
}


And I am attempting to use it in my component as follows:



this.response = this.infodesk.getInfoDeskTopic(this.topicUrl);
this.news = this.response.json;
this.success = this.response.ok;


I have the variable in my component (this.response: any). I receive the error:



Uncaught Error: Can't resolve all parameters for InfoDeskService: ([object Object], ?).
at syntaxError (compiler.es5.js:1540)


Can any help me understand why I am getting an error? I feel like I am declaring something incorrectly perhaps, but I cannot figure out what the issue is. Thanks



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





The following is what I had PREVIOUSLY and it was working fine (just for reference):



In my service:



   getInfoDeskTopic(topicUrl) {
this.infoDeskData = ;
return this.http.get(topicUrl, this.options)
.map(res => res.json().resultSet.results)
.map(data => data
.forEach(item => {
let infoDesk = <InfoDesk>{};
infoDesk.Url = item.json.webUrl;
infoDesk.Date = item.date;
item.fields.forEach(item => {
if (item.id == 'publication') {
infoDesk.Publication = item.strings[0];
}
if (item.id == 'headline') {
infoDesk.Title = item.strings[0];
}
});
this.infoDeskData.push(infoDesk);
}))
.map(data => data = this.infoDeskData)
.map(items => items.slice(0, 5))
.share();
}


And in my component:



this.news = this.infodesk.getInfoDeskTopic(this.topicUrl);









share|improve this question














I had a previously working service that used to only return the JSON response into my component (see second section for previously working code). However, I am building in additional functionality into all of my components to have loading animations and error messages so I need the response.OK and response.status code as well. I created this generic response interface to use in a global variable service I have as follows:



export interface GetResponse  {
status: number,
ok: boolean,
json: any
}


I am using it in my service as follows:



   getInfoDeskTopic(topicUrl) {
this.infoDeskData = ;
this.http.get(topicUrl, this.options)
.map(res => this.response = res)
.map(res => this.response.json = res.json().resultSet.results)
this.response.json
.map(data => data
.forEach(item => {
let infoDesk = <InfoDesk>{};
infoDesk.Url = item.json.webUrl;
infoDesk.Date = item.date;
item.fields.forEach(item => {
if (item.id == 'publication') {
infoDesk.Publication = item.strings[0];
}
if (item.id == 'headline') {
infoDesk.Title = item.strings[0];
}
});
this.infoDeskData.push(infoDesk);
}))
.map(data => data = this.infoDeskData)
.map(items => items.slice(0, 5))
.share();
return this.response;
}


And I am attempting to use it in my component as follows:



this.response = this.infodesk.getInfoDeskTopic(this.topicUrl);
this.news = this.response.json;
this.success = this.response.ok;


I have the variable in my component (this.response: any). I receive the error:



Uncaught Error: Can't resolve all parameters for InfoDeskService: ([object Object], ?).
at syntaxError (compiler.es5.js:1540)


Can any help me understand why I am getting an error? I feel like I am declaring something incorrectly perhaps, but I cannot figure out what the issue is. Thanks



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





The following is what I had PREVIOUSLY and it was working fine (just for reference):



In my service:



   getInfoDeskTopic(topicUrl) {
this.infoDeskData = ;
return this.http.get(topicUrl, this.options)
.map(res => res.json().resultSet.results)
.map(data => data
.forEach(item => {
let infoDesk = <InfoDesk>{};
infoDesk.Url = item.json.webUrl;
infoDesk.Date = item.date;
item.fields.forEach(item => {
if (item.id == 'publication') {
infoDesk.Publication = item.strings[0];
}
if (item.id == 'headline') {
infoDesk.Title = item.strings[0];
}
});
this.infoDeskData.push(infoDesk);
}))
.map(data => data = this.infoDeskData)
.map(items => items.slice(0, 5))
.share();
}


And in my component:



this.news = this.infodesk.getInfoDeskTopic(this.topicUrl);






angular typescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 1 '17 at 17:19









kittycatbyteskittycatbytes

1609




1609













  • I have gotten rid of the error mentioned above by declaring the interface as the type for a variable in the service as opposed to in the constructor of the service. However, now I am getting the error: "Cannot read property 'ok' of undefined" from when I am trying to set the variable in my constructor

    – kittycatbytes
    Aug 1 '17 at 17:40



















  • I have gotten rid of the error mentioned above by declaring the interface as the type for a variable in the service as opposed to in the constructor of the service. However, now I am getting the error: "Cannot read property 'ok' of undefined" from when I am trying to set the variable in my constructor

    – kittycatbytes
    Aug 1 '17 at 17:40

















I have gotten rid of the error mentioned above by declaring the interface as the type for a variable in the service as opposed to in the constructor of the service. However, now I am getting the error: "Cannot read property 'ok' of undefined" from when I am trying to set the variable in my constructor

– kittycatbytes
Aug 1 '17 at 17:40





I have gotten rid of the error mentioned above by declaring the interface as the type for a variable in the service as opposed to in the constructor of the service. However, now I am getting the error: "Cannot read property 'ok' of undefined" from when I am trying to set the variable in my constructor

– kittycatbytes
Aug 1 '17 at 17:40












1 Answer
1






active

oldest

votes


















0














First if you use angular 4+, you should to use HTTPCLIENT vs HTTP.



Please, search the difference.. (You don't need to use .map)



Please look at this example:



interface interfaceJSON {
name: string;
email: string;
company: string;
}

this.http.get<interfaceJSON>('apiUrl').subscribe(data => {
console.log("Name: " + data.name);
console.log("Email: " + data.email);
console.log("Company: " + data.company);
});





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45444092%2fangular-4-return-http-response-as-interface-with-json-as-property%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    First if you use angular 4+, you should to use HTTPCLIENT vs HTTP.



    Please, search the difference.. (You don't need to use .map)



    Please look at this example:



    interface interfaceJSON {
    name: string;
    email: string;
    company: string;
    }

    this.http.get<interfaceJSON>('apiUrl').subscribe(data => {
    console.log("Name: " + data.name);
    console.log("Email: " + data.email);
    console.log("Company: " + data.company);
    });





    share|improve this answer






























      0














      First if you use angular 4+, you should to use HTTPCLIENT vs HTTP.



      Please, search the difference.. (You don't need to use .map)



      Please look at this example:



      interface interfaceJSON {
      name: string;
      email: string;
      company: string;
      }

      this.http.get<interfaceJSON>('apiUrl').subscribe(data => {
      console.log("Name: " + data.name);
      console.log("Email: " + data.email);
      console.log("Company: " + data.company);
      });





      share|improve this answer




























        0












        0








        0







        First if you use angular 4+, you should to use HTTPCLIENT vs HTTP.



        Please, search the difference.. (You don't need to use .map)



        Please look at this example:



        interface interfaceJSON {
        name: string;
        email: string;
        company: string;
        }

        this.http.get<interfaceJSON>('apiUrl').subscribe(data => {
        console.log("Name: " + data.name);
        console.log("Email: " + data.email);
        console.log("Company: " + data.company);
        });





        share|improve this answer















        First if you use angular 4+, you should to use HTTPCLIENT vs HTTP.



        Please, search the difference.. (You don't need to use .map)



        Please look at this example:



        interface interfaceJSON {
        name: string;
        email: string;
        company: string;
        }

        this.http.get<interfaceJSON>('apiUrl').subscribe(data => {
        console.log("Name: " + data.name);
        console.log("Email: " + data.email);
        console.log("Company: " + data.company);
        });






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 12:38









        Sarvan Kumar

        834321




        834321










        answered Nov 24 '18 at 10:56









        GuidoGuido

        1




        1






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45444092%2fangular-4-return-http-response-as-interface-with-json-as-property%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks