TypeError:对象不是函数或其返回值不可迭代.在redux-rtk中,同时尝试使用突变函数



这是我的Apis设置

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
export const bugApis = createApi({
reducerPath: 'Bug',
baseQuery:fetchBaseQuery({
baseUrl: 'http://localhost:5000'
}),
endpoints:(builder) => ({
getBugData: builder.query({
query: () => 'api/v1/bugs'
}),
postNewBug: builder.mutation({
query: (initialPost) => ({
url: 'api/v1/bugs', 
method: 'POST', 
body: initialPost
})
})
})
})
export const { useGetBugDataQuery,  usePostBugMutation } =  bugApis

这是我调用usePostBugMutation()时遇到的错误

import React, { useState } from 'react'
import './form.css'
import FileBase from 'react-file-base64'
import { usePostBugMutation } from '../../services/bugApis'
function Form() {
const [input, setInput] = useState({ author: '', title: '',  message: '', uploadFile: ''})
const [ addNewBug, { isLoading } ] = usePostBugMutation()
const handleInput = (e) => {
e.preventDefault()
setInput({ author: '', title: '',  message: '', uploadFile: ''})
}
return (
<div className='formContainer'>
<h2 className='form-header'>creating new bug</h2>
<div className="formWrapper">
<form className='input-wrapper' onSubmit={handleInput}>
<input type="text" placeholder='author' name='author' onChange={(e) => setInput({...input, author: e.target.value})} />
<input type="text" placeholder='title' name='title' onChange={(e) => setInput({...input, title: e.target.value})} />
<textarea placeholder='description'  name='message' onChange={(e) => setInput({...input, message: e.target.value})} ></textarea>
{/* <input type="text" placeholder='tags (coma separated)'  name='message' onChange={(e) => setInput({...input, message: e.target.value})}/> */}
<FileBase  type='file'  multiple={false}   onDone={({base64}) => setInput({...input, uploadFile: base64 })}
/>
<button type='submit' className='submit-btn' onClick={handleInput}>submit</button>
<button type='reset' className='reset-btn'>clear</button>
</form>
</div>
</div>
)
}
export default Form

就像我破坏了addNewPost()一样,我没有使用它,我得到了这个错误

您的查询名为"postNewBug",但您正试图导出"usePostBugMutuation",您应该导出"usepostNewBugMutution"。看起来你只是错过了"新"。我相信这就是为什么你会得到错误对象不是一个函数,因为"usePostBugMutation"不存在。

最新更新