我正在处理一个登录页面,并试图将我的数据存储在上下文中。但我从数据库得到的响应是未定义的。
我觉得我做错了什么。请帮助
这是我的代码片段"AUTH CONTEXT";(auth-Context文件返回undefined作为响应(
import { signIn as signInApi } from '../apis'
const AuthContext = createContext()
export const AuthProvider = ({ children }) => {
const [token, setToken] = useState(localStorage.getItem('token'))
const [user, setUser] = useState(localStorage.getItem('user'))
const [loading, setLoading] = useState(false)
const signIn = async (email, password, callback) => {
setLoading(true)
const res = await signInApi(email, password)
console.log(res)
......
const value = {
token,
loading,
signIn,
signOut,
}
}
export default AuthContext
APIS.JS(下面的API.JS文件从数据库返回响应数据(
import axios from 'axios'
export const signIn = async (email, password) => {
try {
const res = await axios.post(
`${process.env.REACT_APP_API}/auth/login`,
{
email,
password,
},
{
headers: {
'Content-Type': 'application/json',
},
}
)
} catch (error) {
console.log(error)
}
}
登录文件
const auth = useContext(AuthContext)
const handleLogin = (e) => {
e.preventDefault()
auth.signIn(email, password, () => history.replace('/admin'))
}
您没有从API.js文件中的signIn函数返回响应