Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用express中的函数呈现Jade模板中的内容?_Javascript_Node.js_Express - Fatal编程技术网

Javascript 如何使用express中的函数呈现Jade模板中的内容?

Javascript 如何使用express中的函数呈现Jade模板中的内容?,javascript,node.js,express,Javascript,Node.js,Express,我有一个有效的用户注册/登录页面,唯一剩下的就是根据用户是否登录来制作内容。读完后,我想到了这个: app.use(function(req, res, next) { db.users.find({rememberToken: req.cookies.rememberToken}, function(err, foundUser) { if (err) { next(); } if (foundUser[0].length === 0) { res.local

我有一个有效的用户注册/登录页面,唯一剩下的就是根据用户是否登录来制作内容。读完后,我想到了这个:

app.use(function(req, res, next) {
  db.users.find({rememberToken: req.cookies.rememberToken}, function(err, foundUser) {
    if (err) { next(); }

    if (foundUser[0].length === 0) {
      res.locals.isLoggedIn = false;
      next();
    } else {
      res.locals.isLoggedIn = true;
      next();
    }
  });
});
为了测试它,我在我的Jade模板中做了如下操作:

!!!
html
 head
  #{isLoggedIn()}
但所有发生的事情都是说isLoggedIn没有定义

我找不到关于这个主题的太多文档,所以任何信息都很好。谢谢


我基本上想让函数返回true或false,这样我就可以基于它加载内容。

您可以让它工作:

!!!
html
 head
  | #{isLoggedIn()}
或在条件语句中:

!!!
html
  head
  - if (isLoggedIn())
    // do something
  - else
    // do something else

但是为什么不把
isLoggedIn
变成一个布尔变量而不是一个函数呢?我很确定从Jade调用异步函数不会起作用。

您可以让它起作用:

!!!
html
 head
  | #{isLoggedIn()}
或在条件语句中:

!!!
html
  head
  - if (isLoggedIn())
    // do something
  - else
    // do something else

但是为什么不把
isLoggedIn
变成一个布尔变量而不是一个函数呢?我敢肯定,从Jade调用异步函数是行不通的。

首先,要获得函数的结果,必须调用函数,而不是像您那样渲染元素。要调用函数,请使用以下语法:

p #{isLoggedIn()}
要呈现HTML/jade的一部分,可以使用

- if (isLoggedIn())
  p User is logged in
代码示例的真正问题是,您正在从视图访问数据库,而在视图中使用的回调(next)将不起作用。 一种更好的工作方式是使用一些中间件在res.locals中设置
isLoggedIn
字段

var isLoggedIn = function(req, res, next) {
  db.users.find({rememberToken: req.cookie.rememberToken}, function(err, foundUser) {
    if (err) { console.log('We have an error ' + err ); next();}

    if (foundUser[0].length === 0) {
        console.log('We could not find the user with the rememberToken ' + req.cookie.rememberToken);
        res.locals.isLoggedIn = false;
        next();
    } else {
        console.log('We found the user with the rememberToken ' + req.cookie.rememberToken);
        res.locals.isLoggedIn = true;
        next();
    }
  });
};

app.get('/yourroute', isLoggedIn, routes.yourroute);

首先,要获得函数的结果,您必须调用函数,而不是像您那样渲染元素。要调用函数,请使用以下语法:

p #{isLoggedIn()}
要呈现HTML/jade的一部分,可以使用

- if (isLoggedIn())
  p User is logged in
代码示例的真正问题是,您正在从视图访问数据库,而在视图中使用的回调(next)将不起作用。 一种更好的工作方式是使用一些中间件在res.locals中设置
isLoggedIn
字段

var isLoggedIn = function(req, res, next) {
  db.users.find({rememberToken: req.cookie.rememberToken}, function(err, foundUser) {
    if (err) { console.log('We have an error ' + err ); next();}

    if (foundUser[0].length === 0) {
        console.log('We could not find the user with the rememberToken ' + req.cookie.rememberToken);
        res.locals.isLoggedIn = false;
        next();
    } else {
        console.log('We found the user with the rememberToken ' + req.cookie.rememberToken);
        res.locals.isLoggedIn = true;
        next();
    }
  });
};

app.get('/yourroute', isLoggedIn, routes.yourroute);

检查我编辑的帖子,我试着按照你说的做,但它仍然说isLoggedIn没有定义你需要在使用
isLoggedIn
的路由之前定义你的中间件。检查我编辑的帖子,我试着按照你说的做,但是它仍然说isLoggedIn没有定义你需要在使用
isLoggedIn
的路由之前定义你的中间件,但是现在我得到了这个错误:
500 TypeError:无法读取未定义的属性'rememberToken'
您需要在您自己的中间件之前设置
express.cookieParser
中间件:)谢谢我还有一个问题,如果我现在登录,它会在实际设置cookie之前检查值,所以我必须加倍刷新页面,让它在我登录时显示为真,知道如何修复吗?如果你查看我的原始帖子,我编辑了一点,但是现在我得到了这个错误:
500 TypeError:无法读取未定义的属性'rememberToken'
您需要在您自己的中间件之前设置
express.cookieParser
中间件:)谢谢我还有一个问题,如果我现在登录,它会在实际设置cookie之前检查值,所以我必须加倍刷新页面,以便在登录时显示为真,知道如何修复吗?