Node.js 如果用户身份验证无法正常工作

Node.js 如果用户身份验证无法正常工作,node.js,mongodb,pug,Node.js,Mongodb,Pug,我想使用if user方法进行身份验证。当我执行console.log(user)和cosole.log(kullanici)时,我得到了正确的数据 kullanici指配置文件中显示的用户 用户指当前登录的用户(请求用户) 我的app.js文件: app.get('/kullanici/:id', function(req, res){ Kullanici.findById(req.user ).exec(function(err, user){ Kullanici.fin

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

kullanici指配置文件中显示的用户

用户指当前登录的用户(请求用户)

我的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,
    });
  });
 });
if user
  if user._id == kullanici._id
    a.button.btn.btn-light(href='/kullanici/duzenleme/' + kullanici.id, style="margin-right:85%;")Düzenle
}))

我的kullanici.pug文件:

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,
    });
  });
 });
if user
  if user._id == kullanici._id
    a.button.btn.btn-light(href='/kullanici/duzenleme/' + kullanici.id, style="margin-right:85%;")Düzenle

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

不要在pug中使用默认的
if
表达式,尝试使用JavaScript来计算表达式的真实性。因此,尝试使用
-if(user.\u id==kullanici.\u id
)而不是
if user.\u id==kullanici.\u id)

在哈巴狗标签声明及其内容之间需要一个空格

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

我通过将其添加到我的哈巴狗模板中解决了这个问题==>

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

尝试将
user.\u id
kullanici.\u id
的值和类型都记录到控制台中。如果它们是字符串,还要确保没有前导空格或尾随空格。您还可以记录测试的结果
console.log(user.\u id==kullanici.\u id)
no。它给出了配置文件用户的id和请求用户的id,因此它是真实的。它不起作用。我不知道为什么。我也没有得到错误。(我不确定我是否正确地使用了这两个:)这与此无关。在我的真实脚本中有一个空格。按钮工作正常。@Ö.ALPERENGÜL请确保问题中的代码与实际代码匹配。