Angular http请求返回值与接口不符
我试图理解为什么我的第一个http请求符合我的接口,但我的第二个调用不符合 我的控制器:Angular http请求返回值与接口不符,angular,typescript,promise,interface,httprequest,Angular,Typescript,Promise,Interface,Httprequest,我试图理解为什么我的第一个http请求符合我的接口,但我的第二个调用不符合 我的控制器: this.qs.getCategoryNames() .then((res: AllCategoryTab) => { this.questionCategory = res; this.qs.getSurveyByName(this.questionCategory.names[0]) .then((result: AllQuestions)=> { //result not co
this.qs.getCategoryNames()
.then((res: AllCategoryTab) => {
this.questionCategory = res;
this.qs.getSurveyByName(this.questionCategory.names[0])
.then((result: AllQuestions)=> { //result not conforming to AllQuestion interface
this.allQuestions = result; //allQuestions not conforming to interface
this.categoryIndex = 1;
console.log(typeof this.allQuestions.questions); //show "undefined"
}).catch(function(err){
throw err;
});
})
.catch(function(err) {
return err;
});
我的服务:
getCategoryNames(): Promise<Object> {
const url = `${this.baseUrl}/survey/names`;
return this.http.get(url).toPromise()
.then(function(result) {
console.log(typeof "result 1 ", result) //type is string
return result;
}).catch(function(err) {
throw err;
});
getSurveyByName(name: string): Promise<Object> {
const url = `${this.baseUrl}/survey/${name}`;
return this.http.get(url).toPromise()
.then(function(result) {
console.log(typeof "result 2 ", result) // type is string
return result;
}).catch(function(err) {
throw err;
});
getCategoryNames():承诺{
constURL=`${this.baseUrl}/survey/names`;
返回此.http.get(url.toPromise())
.然后(函数(结果){
console.log(typeof“result 1”,result)//类型为string
返回结果;
}).catch(函数(err){
犯错误;
});
getSurveyByName(名称:string):承诺{
常量url=`${this.baseUrl}/survey/${name}`;
返回此.http.get(url.toPromise())
.然后(函数(结果){
console.log(typeof“result 2”,result)//类型为string
返回结果;
}).catch(函数(err){
犯错误;
});
从理论上讲,接口看起来就像我从服务器检索的数据:
export interface AllCategoryTab {
names: string
}
export interface Questions {
answered: boolean,
name: string,
question: string,
types: Array<string>,
value: boolean
}
export interface Condition {
name: string,
number: number,
value: boolean
}
export interface ConditionsObject {
condition: Array<Condition>
}
export interface AllQuestions {
questions: Array<Questions>,
conditions:Array<ConditionsObject>,
potentialDisorders: string
}
导出接口AllCategoryTab{
名称:字符串
}
导出接口问题{
回答:布尔,
名称:string,
问题:字符串,,
类型:数组,
值:布尔值
}
导出接口条件{
名称:string,
号码:号码,,
值:布尔值
}
导出接口条件对象{
条件:数组
}
导出接口所有问题{
问题:数组,
条件:数组,
潜在障碍:字符串
}
因为我使用的是typescript,所以所有类型都是预先设置好的,但是当我尝试将我的承诺的结果放入变量时,它似乎停留在我接收它的方式上,它不会转换为我想要的样子。typescript在运行时不存在。它不能进行任何转换或转换。你必须告诉它你实际上要做什么接收,而不是你想要的。所以,我应该在我的服务中接收所有准备好的东西,而不是将结果放入我控制器中的虚构变量中。ps:我理解错了,我的界面是按照我服务器中数据的外观制作的,所以它不是我真正“想要”的,但它也是我期望的样子