reactjs - 在辅助函数中调用 axios 时无法读取属性“then”
问题描述
我将 axios 调用分离到另一个名为 ApiMethods.js 的文件中。
现在,每当我尝试在 ApiMethods.js 中使用“uploadSpecFile”函数时,都会收到以下错误:
未捕获的类型错误:无法读取未定义的属性“then”
这是辅助方法:
export function uploadSpecFile(formData, engineId) {
return
axios({
method: 'post',
url: `/api/engineData/spec-files/${engineId}/files/upload`,
data: formData,
config: {
headers: { 'Content-Type': 'multipart/form-data' }
}
})
}
我在这里使用它:
import { uploadSpecFile } from '/ApiMethods';
// error occurs here **
uploadSpecFile(bodyFormData, this.props.engineId).then(response => {
this.setState({
selectedFile: response.data,
file: null
});
})
.catch(response => {
});
有什么我做错了吗?
谢谢!
解决方案
您正在返回undefined
,因为axios
与 return 语句不在同一行。
只需将axios
请求移到同一行,它就会按预期工作。
export function uploadSpecFile(formData, engineId) {
return axios({
method: "post",
url: `/api/engineData/spec-files/${engineId}/files/upload`,
data: formData,
config: {
headers: { "Content-Type": "multipart/form-data" }
}
});
}
推荐阅读
- javascript - 使用来自另一个通过 ID 链接的数据更新一个 JSON blob 中的数据元素
- elixir - Ecto - 按月访问日期时间字段
- smtp - DNN 9 SMTP 用户名和密码已自动更改
- c# - 如何使用 C# MongoDB.Driver 更新深度嵌套的数组?
- spring - @Controller Kafka Listener 异常未被异常处理程序 @ControllerAdvice 捕获
- google-app-engine - Google App 引擎上的生产部署问题
- python - Python:在句子列表中查找常见句子片段
- css - 带有 CSS 和媒体查询的浮动文本容器
- javascript - 在触摸设备上平移视口
- laravel - 在 Laravel 的 FormRequest 中使用复杂的条件验证规则