【问题标题】:How to get user information from a given ID and display it using Node.js and EJS [closed]如何从给定 ID 获取用户信息并使用 Node.js 和 EJS 显示它[关闭]
【发布时间】:2015-01-26 00:21:23
【问题描述】:

我有一个使用 EJS 模板的主页,
我想使用 URL 中的用户 id 显示用户名(不是现在登录的用户)

页面是这样调用的:

/home?id=235325325235

这里是 home.ejs 的一部分:

<!-- i want to get the username or other info of another user -->
<p> Hello <%= get.otheruser.name(id) %> </p>

这是我的 routes.js 的一部分

// load up the user model
var User       = require('../app/models/user');

app.get('/home', function(req, res) {
    res.render('home.ejs', {
        id              : req.query.id,
        otheruser       : User.findById( req.query.id ), //error this doesn't get the info i need
        currentuser     : req.user  //ignore this, this is the information of the current logged in user
    });
});

但是,如果登录的用户想要查看其他用户的主页会怎样?

他会用其他用户的id调用主页,

/home?id=43565476535

我需要一个使用 URL 中的 ID 获取所有信息的函数

node.js 是如何做到这一点的?

【问题讨论】:

    标签: node.js mongodb function mongoose ejs


    【解决方案1】:

    为了回答我的问题,我找到了办法。

    app.get('/home', function(req, res) {
        //get user information based on the id url parameter
        User.findById(req.query.id, function(err, doc) {
                res.render('home.ejs', {
                    id     : doc._id
                });     
        });
    });
    

    它需要 findById 来使用 URL 中的 id 获取用户 id。

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      相关资源
      最近更新 更多