typescript - 在 React Native 中处理 API 调用?
问题描述
我是本机反应的新手,并试图与我在我的一个屏幕文件中创建的 API 帮助程序进行交互,我确信这是一个语法错误,但我也很感兴趣它的架构是否正确,因为我来了形成iOS开发。
有错误的函数:
handleLoginPress = () => {
ApiHelper.loginDriver(this.state.email, this.state.password)
.then((result) => {
console.log(result);
}).catch((error) => {
console.log("error is " + error);
});
};
错误是:
“void”类型上不存在属性“then”
它的调用函数:
static loginDriver(username: string, password: string) {
fetch('http://localhost:8080/drivers/login', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
uername: username,
password: password,
}),
});
}
我的想法是需要对 apis 登录函数的返回类型进行一些规范吗?
解决方案
您的 loginDriver 函数中缺少 return,请添加 return 语句以返回数据。
static loginDriver(username: string, password: string) {
return fetch('http://localhost:8080/drivers/login', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
uername: username,
password: password
})
});
}
推荐阅读
- c# - Really struggling to get how WiX works
- c++ - C++ How to stop private member variables constructor being called. It supposed to reference another object
- android - Why the Facebook login popup opened on an external web browser app on my android device
- javascript - 选择多个选项并单击提交后使复选框变灰
- php - 在while循环中使用foreach循环获取每行下的所有子列表项
- python - Python:计算字符串开头的连续字符
- java-8 - 在 Java 8 中对复杂的解引用链进行空检查
- python - mysql.connector 系统错误问题:32 Broken Pipe
- sql - 相同的 SQL 查询适用于某些表,但在同一数据库中相同的其他表上出错
- google-apps-script - 我想在 Google Sheet 宏中使用 Autocrat