ionic Assign HttpClient response variables
I'm learning the ionic framework and want to know how REST calls are done correctly.
I have a RestProvider class which gets me a list of products
rest.ts
getProduct(query: string) {
return this.http.get(searchUri+query)
}
And in my home page I want to assign a value from the response to a local variable
home.ts
ionViewDidLoad() {
this.restProvider.getProduct(query).subscribe(it => {
console.log(it)
this.products = it.result;
})
}
But I get an error on assignment line this.products = it.result;
[ts] Property result does not exist on type Object
How do I create models of my response and assign them to class variables?
angular ionic-framework ionic2 ionic3
add a comment |
I'm learning the ionic framework and want to know how REST calls are done correctly.
I have a RestProvider class which gets me a list of products
rest.ts
getProduct(query: string) {
return this.http.get(searchUri+query)
}
And in my home page I want to assign a value from the response to a local variable
home.ts
ionViewDidLoad() {
this.restProvider.getProduct(query).subscribe(it => {
console.log(it)
this.products = it.result;
})
}
But I get an error on assignment line this.products = it.result;
[ts] Property result does not exist on type Object
How do I create models of my response and assign them to class variables?
angular ionic-framework ionic2 ionic3
check if 'it' contains a result property, if so check r u accessing that in a valid way
– Sa E Chowdary
Nov 12 '18 at 10:57
'it' is a JSON response and 'it.result' isn't explicitly defined anywhere, how can I do that?
– ir2pid
Nov 12 '18 at 11:05
1
That'll be because of Typescript. Change the service tothis.http.get<any>(searchUri+query)
and it should work. Better yet, if you have an interface that represents the response, use that instead ofany
– user184994
Nov 12 '18 at 11:09
add a comment |
I'm learning the ionic framework and want to know how REST calls are done correctly.
I have a RestProvider class which gets me a list of products
rest.ts
getProduct(query: string) {
return this.http.get(searchUri+query)
}
And in my home page I want to assign a value from the response to a local variable
home.ts
ionViewDidLoad() {
this.restProvider.getProduct(query).subscribe(it => {
console.log(it)
this.products = it.result;
})
}
But I get an error on assignment line this.products = it.result;
[ts] Property result does not exist on type Object
How do I create models of my response and assign them to class variables?
angular ionic-framework ionic2 ionic3
I'm learning the ionic framework and want to know how REST calls are done correctly.
I have a RestProvider class which gets me a list of products
rest.ts
getProduct(query: string) {
return this.http.get(searchUri+query)
}
And in my home page I want to assign a value from the response to a local variable
home.ts
ionViewDidLoad() {
this.restProvider.getProduct(query).subscribe(it => {
console.log(it)
this.products = it.result;
})
}
But I get an error on assignment line this.products = it.result;
[ts] Property result does not exist on type Object
How do I create models of my response and assign them to class variables?
angular ionic-framework ionic2 ionic3
angular ionic-framework ionic2 ionic3
asked Nov 12 '18 at 10:54
ir2pidir2pid
97011436
97011436
check if 'it' contains a result property, if so check r u accessing that in a valid way
– Sa E Chowdary
Nov 12 '18 at 10:57
'it' is a JSON response and 'it.result' isn't explicitly defined anywhere, how can I do that?
– ir2pid
Nov 12 '18 at 11:05
1
That'll be because of Typescript. Change the service tothis.http.get<any>(searchUri+query)
and it should work. Better yet, if you have an interface that represents the response, use that instead ofany
– user184994
Nov 12 '18 at 11:09
add a comment |
check if 'it' contains a result property, if so check r u accessing that in a valid way
– Sa E Chowdary
Nov 12 '18 at 10:57
'it' is a JSON response and 'it.result' isn't explicitly defined anywhere, how can I do that?
– ir2pid
Nov 12 '18 at 11:05
1
That'll be because of Typescript. Change the service tothis.http.get<any>(searchUri+query)
and it should work. Better yet, if you have an interface that represents the response, use that instead ofany
– user184994
Nov 12 '18 at 11:09
check if 'it' contains a result property, if so check r u accessing that in a valid way
– Sa E Chowdary
Nov 12 '18 at 10:57
check if 'it' contains a result property, if so check r u accessing that in a valid way
– Sa E Chowdary
Nov 12 '18 at 10:57
'it' is a JSON response and 'it.result' isn't explicitly defined anywhere, how can I do that?
– ir2pid
Nov 12 '18 at 11:05
'it' is a JSON response and 'it.result' isn't explicitly defined anywhere, how can I do that?
– ir2pid
Nov 12 '18 at 11:05
1
1
That'll be because of Typescript. Change the service to
this.http.get<any>(searchUri+query)
and it should work. Better yet, if you have an interface that represents the response, use that instead of any
– user184994
Nov 12 '18 at 11:09
That'll be because of Typescript. Change the service to
this.http.get<any>(searchUri+query)
and it should work. Better yet, if you have an interface that represents the response, use that instead of any
– user184994
Nov 12 '18 at 11:09
add a comment |
1 Answer
1
active
oldest
votes
The object you get in return is a JSON parsing of the response body. The error is that Typescript does now know if the result
property exists on the returned type. So you should disambiguate the situation.
You declare the type of the returned object with the template argument to get
as in get<MyReturnedType>
. So here, you should provide a template argument containing the result
property.
Either you know the type for it, then use it, or you don't, then use any
and you are responsible for the object accesses you make.
You can get more details on this Angular documentation page.
Note: don't use searchUri+query
as your url because it is not escaped properly. You should instead use HttpParams
described here.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260637%2fionic-assign-httpclient-response-variables%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
The object you get in return is a JSON parsing of the response body. The error is that Typescript does now know if the result
property exists on the returned type. So you should disambiguate the situation.
You declare the type of the returned object with the template argument to get
as in get<MyReturnedType>
. So here, you should provide a template argument containing the result
property.
Either you know the type for it, then use it, or you don't, then use any
and you are responsible for the object accesses you make.
You can get more details on this Angular documentation page.
Note: don't use searchUri+query
as your url because it is not escaped properly. You should instead use HttpParams
described here.
add a comment |
The object you get in return is a JSON parsing of the response body. The error is that Typescript does now know if the result
property exists on the returned type. So you should disambiguate the situation.
You declare the type of the returned object with the template argument to get
as in get<MyReturnedType>
. So here, you should provide a template argument containing the result
property.
Either you know the type for it, then use it, or you don't, then use any
and you are responsible for the object accesses you make.
You can get more details on this Angular documentation page.
Note: don't use searchUri+query
as your url because it is not escaped properly. You should instead use HttpParams
described here.
add a comment |
The object you get in return is a JSON parsing of the response body. The error is that Typescript does now know if the result
property exists on the returned type. So you should disambiguate the situation.
You declare the type of the returned object with the template argument to get
as in get<MyReturnedType>
. So here, you should provide a template argument containing the result
property.
Either you know the type for it, then use it, or you don't, then use any
and you are responsible for the object accesses you make.
You can get more details on this Angular documentation page.
Note: don't use searchUri+query
as your url because it is not escaped properly. You should instead use HttpParams
described here.
The object you get in return is a JSON parsing of the response body. The error is that Typescript does now know if the result
property exists on the returned type. So you should disambiguate the situation.
You declare the type of the returned object with the template argument to get
as in get<MyReturnedType>
. So here, you should provide a template argument containing the result
property.
Either you know the type for it, then use it, or you don't, then use any
and you are responsible for the object accesses you make.
You can get more details on this Angular documentation page.
Note: don't use searchUri+query
as your url because it is not escaped properly. You should instead use HttpParams
described here.
answered Nov 12 '18 at 11:20
MicMic
1,95711531
1,95711531
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260637%2fionic-assign-httpclient-response-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
check if 'it' contains a result property, if so check r u accessing that in a valid way
– Sa E Chowdary
Nov 12 '18 at 10:57
'it' is a JSON response and 'it.result' isn't explicitly defined anywhere, how can I do that?
– ir2pid
Nov 12 '18 at 11:05
1
That'll be because of Typescript. Change the service to
this.http.get<any>(searchUri+query)
and it should work. Better yet, if you have an interface that represents the response, use that instead ofany
– user184994
Nov 12 '18 at 11:09