TypeError: Cannot read properties of undefined (reading 'set')
TypeError: Cannot read properties of undefined (reading 'set')
虽然我正在尝试开发 mern ec 站点。
登录页面出现此错误。
我试过的
1 Cookies->Cookie,设置->设置
2 res.refresh_token->'res.refresh_token'
3 将 js-cookies 版本从 3 降级到 2.2.1
Cookies.set('refreshtoken', res.refresh_token, {
path: 'api/auth/accessToken',
expires: 7
})
所有代码(html除外)
import Head from "next/head"
import Link from "next/link"
import { useState, useContext } from "react"
import { DataContext } from "../store/GlobalState"
import { postData } from "../utils/fetchData"
import { Cookies } from 'js-cookie'
const Signin = () => {
const initialState = { email: '', password: '' }
const [userData, setUserData] = useState(initialState)
const { email, password } = userData
const { state, dispatch } = useContext(DataContext)
const handleChangeInput = e => {
const { name, value } = e.target
setUserData({ ...userData, [name]: value })
dispatch({ type: 'NOTIFY', payload: {} })
}
const handleSubmit = async e => {
e.preventDefault()
dispatch({ type: 'NOTIFY', payload: { loading: true } })
const res = await postData('auth/login', userData)
if (res.err) return dispatch({ type: 'NOTIFY', payload: { error: res.err } })
dispatch({ type: 'NOTIFY', payload: { success: res.msg } })
dispatch({
type: 'AUTH', payload: {
token: res.access_token,
user: res.user
}
})
Cookies.set('refreshtoken', res.refresh_token, {
path: 'api/auth/accessToken',
expires: 7
})
localStorage.setItem('firstLogin', true)
}
}
export default Signin
根据 js-cookie 的文档,您的导入语句有一点偏差。试试这个:
import Cookies from 'js-cookie'
您导入它的方式,它是 return 未定义的,因为 Cookies
对象是默认导入,而不是嵌套在模块导出中。
虽然我正在尝试开发 mern ec 站点。 登录页面出现此错误。
我试过的
1 Cookies->Cookie,设置->设置
2 res.refresh_token->'res.refresh_token'
3 将 js-cookies 版本从 3 降级到 2.2.1
Cookies.set('refreshtoken', res.refresh_token, {
path: 'api/auth/accessToken',
expires: 7
})
所有代码(html除外)
import Head from "next/head"
import Link from "next/link"
import { useState, useContext } from "react"
import { DataContext } from "../store/GlobalState"
import { postData } from "../utils/fetchData"
import { Cookies } from 'js-cookie'
const Signin = () => {
const initialState = { email: '', password: '' }
const [userData, setUserData] = useState(initialState)
const { email, password } = userData
const { state, dispatch } = useContext(DataContext)
const handleChangeInput = e => {
const { name, value } = e.target
setUserData({ ...userData, [name]: value })
dispatch({ type: 'NOTIFY', payload: {} })
}
const handleSubmit = async e => {
e.preventDefault()
dispatch({ type: 'NOTIFY', payload: { loading: true } })
const res = await postData('auth/login', userData)
if (res.err) return dispatch({ type: 'NOTIFY', payload: { error: res.err } })
dispatch({ type: 'NOTIFY', payload: { success: res.msg } })
dispatch({
type: 'AUTH', payload: {
token: res.access_token,
user: res.user
}
})
Cookies.set('refreshtoken', res.refresh_token, {
path: 'api/auth/accessToken',
expires: 7
})
localStorage.setItem('firstLogin', true)
}
}
export default Signin
根据 js-cookie 的文档,您的导入语句有一点偏差。试试这个:
import Cookies from 'js-cookie'
您导入它的方式,它是 return 未定义的,因为 Cookies
对象是默认导入,而不是嵌套在模块导出中。