我正在构建一个使用JWT进行身份验证的银行应用程序.提交登录表单后,服务器给出了响应,但由于某种原因,我无法从服务器获取数据,它一直给出不确定的信息,这很奇怪,因为我可以清楚地看到服务器正在给出响应.

下面是我获取数据的方式

onClick={(event)=>{
                            event.preventDefault()
                            let user={tel, password}
                            fetch('http://localhost:3000/login', {
                                method:'POST',
                                headers:{'Content-Type':'application/json'},
                                body:JSON.stringify(user)
                            })
                                .then(response=>(response.json()))
                                .then(data=>localStorage.setItem('user',JSON.stringify(data)))
                                .then(navigate('/home'))
                                .then(window.location.reload)
                                .catch(error=>{console.log('error sending data to server')})
                        }}

当我没有离开页面时,我可以在空白页面上看到服务器生成的令牌

当前在Express服务器上处理邮件的API

app.post('/login', (req,res,next)=>{
    let loginuser=req.body
    console.log(loginuser)
    let founduser
    Account.find({tel:loginuser.tel}).exec().then(response=>{founduser=response.lastname})
    if (loginuser){
        Account.find({tel:loginuser.tel}, ['userpassword']).exec().then(response=>{
            //New users will require bcrypt.compare() to log-in
            if(response.length!== 0){
                if(loginuser.password===response[0].userpassword){
                console.log('found user')
                res.setHeader('Access-Control-Allow-Origin', '*');
                res.json({token:jwt.sign({lastname:founduser},SECRET)})
                next()
            }
            else{
                console.log('hey! check input')
                res.json({errormsg:'Phone number and password mismatch'})
            }}
            else {
                console.log('user not found')
                res.json({errormsg:'User not found'})
            }
        })
    }
})

来自服务器的响应


{ tel: '0123456789', password: '' }
found user

浏览器控制台控制台

error sending data to server

从上面看,有一些东西触发了Fetch API的错误处理程序,有人能解释一下为什么会这样吗?我做错了什么,我非常感激任何帮助

我试过return fetch('')次了 添加setTimeout()以显示读取的数据 我正在使用带有node/exts.js的reactjs,Mongoose使用ODM

推荐答案

问题就在这一行:

 .then(window.location.reload)

您需要将其更改为:

 .then(() => window.location.reload())

then方法要求将函数作为参数传入.当您直接传递window.location.reload时,您是在传递对window.location对象的reload()方法的引用.然而,then方法期望传入一个函数,因此它try 将window.location.reload()作为函数调用,这会导致非法调用错误.

Reactjs相关问答推荐

在Reaction常量函数中未更新状态变量

使用筛选器的Primereact DataTable服务器端分页?

错误./src/TopScroll.js 31:21-31导出';(作为';随路由导入)在';Reaction-Router-Dom';中找不到

React Context未正确更新

如何在React组件中自动更新图表数据?

React 错误: 只能用作 元素的子元素,从不直接渲染.请将您的 包裹在

无法通过 fetch 获取数据到上下文中

Next.js `getLayout` 函数没有被调用

将 useRef 与 useImperativeHandle 一起使用时,React 不会重新渲染

使用D3.js绘制和展示弦图的右下方象限

部署到github pages时请求路径错误

页面不显示

Select 建议后如何关闭 vscode 添加自动完成={}

React + i18next + Trans + Prettier:Prettier 最终会在重新格式化 JSX 时 destruct 翻译

如何禁用来自 React Native API 的某些特定项目的可touch 不透明度

.filter() 函数在删除函数中创建循环 - React

如何在 v6.4 中将 Auth0 提供程序与新的 createBrowsereRouter 一起使用?

您无权使用阻止 webRequest 侦听器.请务必在 list 中声明 webRequestBlocking 权限

仅在重新渲染后设置状态

在 redux 状态更改时更新react 按钮禁用状态