【问题标题】:If User Authentication Cannot Work Propoerly如果用户验证无法正常工作
【发布时间】:2020-06-20 15:26:26
【问题描述】:

我想使用 if user 方法进行身份验证。当我执行 console.log(user) 和 cosole.log(kullanici) 时,我得到了正确的数据。

kullanici 是指个人资料中显示的用户。

user 表示当前登录的用户(req.user)

我的 app.js 文件:

app.get('/kullanici/:id', function(req, res){
  Kullanici.findById(req.user
    ).exec(function(err, user){
    Kullanici.findById(req.params.id, function(err, kullanici){
      res.render("kullanici",{
      kullanici:kullanici,
      user:user,
    });
  });
 });

});

我的 kullanici.pug 文件:

if user
  if user._id == kullanici._id
    a.button.btn.btn-light(href='/kullanici/duzenleme/' + kullanici.id, style="margin-right:85%;")Düzenle

但我不明白为什么它不起作用。有人可以帮我解决这个问题吗:

【问题讨论】:

  • 尝试控制台记录user._idkullanici._id 的值和类型。如果它们是字符串,还要确保没有前导或尾随空格。您还可以记录测试结果console.log(user._id == kullanici._id)
  • 不,它给出了配置文件用户的 ID 和 req.user 的 ID
  • 原来如此

标签: node.js mongodb pug


【解决方案1】:

不要在 pug 中使用默认的 if 表达式,而是尝试使用 JavaScript 来评估表达式的真实性。因此,请尝试使用 - if(user._id == kullanici._id) 而不是 if user._id == kullanici._id

【讨论】:

  • 它不起作用。我不知道为什么。我也没有收到错误。(我不确定我是否正确使用了:)
【解决方案2】:

Pug 标签声明和它的内容之间需要有一个空格。

a(href='#')Content //incorrect
a(href='#') Content //correct

在这种情况下,您需要在右括号后(在 Düzenle 之前)添加一个空格,如下所示:

if user
  if user._id == kullanici._id
    a.button.btn.btn-light(href='/kullanici/duzenleme/' + kullanici.id, style="margin-right:85%;") Düzenle

【讨论】:

  • 不是这样的。在我的真实脚本中,有一个空格。按钮工作正常。
  • @Ö.ALPERENGÜL 请确保您问题中的代码与您的真实代码相符。
【解决方案3】:

我通过将它添加到我的哈巴狗模板来解决它===>

if user
  if user._id == ""+kullanici._id+""
    a.button.btn.btn-light(href='/kullanici/duzenleme/' + kullanici.id, style="margin-right:85%;")Düzenle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多