GetNextdaySchoolMeal():Array<string>{
fetch(this.GetLink())
.then((response) => {
return(response.json());
})
.then((response) => {
return(response.mealServiceDietInfo[1].row[0].DDISH_NM.split("<br/>"));
})
.catch(function(error){throw new Error(error);});
}
error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
this.GetLink()
返回下一个工作日的 API 链接。为什么不能response.mealServiceDietInfo[1].row[0].DDISH_NM.split("<br/>")
从第二个then
回调中返回GetNextdaySchoolmeal
?
如果我console.log
在第七行放置而不是 return 并删除第一行中的类型,它将按预期工作。但是,我希望该函数返回一个数组。
首先,fetch
返回一个承诺。GetNextdaySchoolMeal
应该返回Promise<Array<string>>
。
一旦你解决了这个问题,你需要返回承诺。return
在之前添加fetch
:
GetNextdaySchoolMeal(): Promise<Array<string>> {
return fetch(this.GetLink())
.then((response) => {
return(response.json());
})
.then((response) => {
return(response.mealServiceDietInfo[1].row[0].DDISH_NM.split("<br/>"));
})
.catch(function(error){throw new Error(error);});
}
有关异步函数和承诺的更多信息:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句