我正在用外部rest构建。
我已经创建了可重用的api调用:
// axiosAPi.js
export const axiosApi = async (method, url, obj = {}) => {
try {
switch (method) {
case 'GET':
return await axios
.get(`${baseUrl}/${url}`, config)
.then((res) => res.data)
case 'POST':
return await axios
.post(`${baseUrl}/${url}`, obj, config)
.then((res) => res.data)
case 'PUT':
return await axios
.put(`${baseUrl}/${url}`, obj, config)
.then((res) => res.data)
case 'DELETE':
return await axios
.delete(`${baseUrl}/${url}`, config)
.then((res) => res.data)
}
} catch (error) {
throw error?.response?.data?.error
}
}我使用react query创建了一个带有登录实例的钩子:
// api/index.js
export default function useApiHook() {
const login = useMutation((obj) => axiosApi('POST', `auth/login`, obj))
return { login }
}以下是登录屏幕的实现
// screens/login.js
const loginPostMutation = useApiHook()?.login
const submitHandler = (data) => {
loginPostMutation
?.mutateAsync(data)
?.then((res) => res)
.catch((err) => err)
}当我发送正确的凭据时,is返回没有错误的数据,但当我发送不正确的凭据时,它返回控制台中的错误+此警告:
node_modules/@tanstack/query-core/build/lib/mutation.js:153:10上的无效凭据( Mutation#execute )
发布于 2022-10-15 08:16:13
所讨论的行指向在开发模式下将错误记录到控制台的react query日志。因此,Invalid credentials只是从axios返回的一个错误,很可能是一个401 -未经授权的错误。
在发出axios请求时,您很可能会得到相同的错误,而不需要进行反作用查询。
https://stackoverflow.com/questions/74053269
复制相似问题