node.js - 将数据发送到 Express 中的前端并访问它

标签 node.js express

我是新手,对如何在前端访问数据感到很困惑,

我有以下代码

exports.init = function(req, res){
  if (req.isAuthenticated()) {
    //res.send();  
    var userId = req.user._id  
    res.render('account/usernotes');
  }
  else {
    res.render('signup/index', {
      oauthMessage: '',
      oauthTwitter: !!req.app.config.oauth.twitter.key,      
      oauthFacebook: !!req.app.config.oauth.facebook.key,
      oauthGoogle: !!req.app.config.oauth.google.key
    });
  }
};

我想将 UserId 发送到 View ,以便我可以访问它,以便我可以使用它获取数据,但到目前为止使用 send();把数据的值渲染到前端,那我怎么访问呢。我猜

res.render('account/usernotes',req.user._id);

应该可以,但是我如何在这之后访问数据?

最佳答案

当我使用 Jade/EJS 模板时,它是这样的:

res.render('account/usernotes', {userid: req.user._id});

在 Jade 文件中:

p #{userid}

在ejs文件中你可以做:

<%=userid%>

关于node.js - 将数据发送到 Express 中的前端并访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25905081/

相关文章:

Android Volley : POST request - req. NodeJS REST API 内部为空

javascript - 松弛事件 api 通过一条消息触发多次

mysql - 如何配置 sequelize 和 node mysql 以使用新的 aws rds 根证书 rds-ca-2019

javascript - 火存储错误:"Nested arrays are not supported"。 Firestore/p5.js 问题

node.js - 使用 Mongoose 在投影中包含 Mongodb "_id"字段

node.js - 让 Node 进行 'understand' ES6 导入 (Babel)

javascript - app.get() 不工作。与 express.js 中 app.use 和 app.get 的区别相关

javascript - Node js/js : Text not appended in file in sequence

node.js - 过期的 Node session cookie

css - node.js 不会在所有页面加载 css 样式