我不明白这里有什么问题。 我收到消息:TS2366:函数缺少结束返回语句并且返回类型不包括“未定义”。
我有一个承诺,我会在任何地方提供数字类型的回报。
有人知道吗?
export async function setAdress(newAdress: IAdress):Promise<number> {
try {
await axios.post<IAddAdressResult>('http://localhost:8090/rest/web/api/addAdress', newAdress)
.then(res=> {
if (res.data && (res.data as IAddAdressResult)) {
alert('Address written successfully. ' + res.data.ADRESSID.toString());
return res.data.ADRESSID
} else return 0
})
.catch(error => {
alert('Error writing the address ' + error);
return 0
});
} catch (e) {
return 0
}
}
答案 0 :(得分:0)
您需要返回整个 Promise 链。照原样,setAdress
函数不返回任何内容。
您还应该考虑将拼写修正为 address
(拼写错误是编程中常见的错误来源),如果您还有 try/catch
,则 .catch
是多余的。>
export async function setAdress(newAdress: IAdress): Promise<number> {
return axios.post<IAddAdressResult>('http://localhost:8090/rest/web/api/addAdress', newAdress)
.then(res => {
if (res.data && (res.data as IAddAdressResult)) {
alert('Address written successfully. ' + res.data.ADRESSID.toString());
return res.data.ADRESSID
} else return 0
})
.catch(error => {
alert('Error writing the address ' + error);
return 0
});
}