javascript - 我正在使用 axios 在 React 中制作一个 CRUD Web 应用程序。我收到以下错误:“TypeError: Object(...) is not a function”
问题描述
错误: TypeError:Object(...) 不是函数
这是我的代码:
api .js 文件
import axios from "axios";
//url call to the server
const baseUrl = "http://localhost:9000/postmessages";
export default {
postMessage(url = baseUrl) {
return {
fetchAll: () => axios.get(url),
fetchById: (id) => axios.get(url + id)
}
}
}
这是postMessage .js 文件
//import api module
import api from "./api";
//fetchall records with a get request
export const fetchall = () => (dispatch) => {
api()
.postMessage()
.fetchall()
.then((res) => {
console.log("postMessage api");
dispatch({
type: ACTION_TYPES.FETCH_ALL,
payload: res.data,
});
})
.catch((err) => console.log(err));
};
我在做什么错或者有什么比我在做什么更好的替代方法
解决方案
在你api.js
导出一个对象:
export default { //this is an exported object not function
...
}
然后您将其称为函数 using ()
,因此您必须删除它们并像这样使用它:
api.postMessage()...
详细的 :
//import api module
import api from "./api";
//fetchall records with a get request
export const fetchall = () => (dispatch) => {
api // omit the ()
.postMessage()
.fetchall()
.then((res) => {
console.log("postMessage api");
dispatch({
type: ACTION_TYPES.FETCH_ALL,
payload: res.data,
});
})
.catch((err) => console.log(err));
};
推荐阅读
- php - 如何找到您的 Apache 正在使用的 PHP.ini - Mac OS Mojave?
- twilio - 无法将媒体发送到 Twilio Whatsapp 沙箱
- node-sass - 如何解决 Node:SCSS 问题?任何解决方案
- android - 我无法在 React Native 中设置状态
- assembly - 你如何在nasm中启用a20?
- android - android确实迁移到房间需要卸载应用程序
- oracle - ALM Rest API - 列表中的最大表达式数为 1000
- html - 为什么在尝试将其移动到左侧时,导航栏项目会与徽标一起移动?
- python - 读取文件并检查文件中是否有数据。Python
- python - 重复的日志输出,但没有重复的处理程序