首页 > 解决方案 > 我的节点应用程序在 localhost 上运行良好,但是当我部署在 heroku 上时,它给了我应用程序错误。你可以阅读下面的描述

问题描述

我有一个带有 express 后端的节点应用程序,我在前端使用 ejs。我的应用程序也有身份验证、登录和注册页面。

我的节点应用程序在 localhost 上运行良好,但是当我部署在 heroku 上时,它给了我应用程序错误——

应用程序错误 应用程序发生错误,无法提供您的页面。如果您是应用程序所有者,请查看您的日志以获取详细信息。您可以从 Heroku CLI 使用命令 heroku logs --tail 执行此操作

当我在我的 vs 代码终端上运行 heroku logs --tail 时,它向我显示 -

**2021-05-16T04:15:48.851524+00:00 app[web.1]:TypeError:无法读取 null 2021-05-16T04:15:48.851525+00:00 app[web.1] 的属性“密码” ]:在 /app/routes/authRoutes.js:37:17

这是我的 authRoutes 代码-

// login for existing user
router.post('/login',async(req,res)=>{

    await User.findOne({email:req.body.email},(err,user)=>{

        // verfying email id
        if(err) return res.send({msg:"email id does not exists"})

        //verfying password
        if(user.password!=req.body.password) return res.send({msg:"password is wrong"})
        else return res.send({_id:user._id,msg:"verified"});
    })
})

我的应用程序部署在链接上 - https://facechatapp.herokuapp.com/ 你可以去看看。

即使我第一次运行我的应用程序,它也可以正常运行,但是当我尝试在手机的 chrome 浏览器上使用我的应用程序时,它会将此消息显示为网页-

应用程序发生错误,无法提供您的页面。如果您是应用程序所有者,请查看您的日志以获取详细信息。您可以从 Heroku CLI 使用命令 heroku logs --tail 执行此操作

如果您需要更多信息,请告诉我。我知道还有其他类似的问题,但作为一个新手,我无法弄清楚我的问题提前谢谢

标签: javascriptnode.jsherokumongodb-atlas

解决方案


我认为可能有 2 个错误.. 但是,我非常有信心这个错误是由于可能的错误编号 2

可能的错误 1

在您的用户模型中,您已经设置了密码字段select:false,密码字段看起来像这样

    password: {
      type: String,
      require: [true, "Please provide a Password"],
      minlength: 6,
      select: false,
    },

如果是这种情况,那么您User.findone()缺少一个关键选项,即.select('password')

您的查询应该是这样的

    User.findOne({email:req.body.email})
        .select('password')
        .then((usr)=>{
            //... Do you Stuff Here
        }).catch((err)=> {
           return res.send({msg:"password is wrong"})
        })

可能的错误 2

您没有在请求中使用正确的键:值对发送电子邮件我建议您仔细检查您的 ajax/axios,我之所以这么说是因为根据错误它试图在 null 上找到密码字段,这仅意味着它是无法找到使用该电子邮件的用户。在这种情况下,这可能是格式错误的..或者由于错误的 key:val 对而根本不存在..


推荐阅读