【问题标题】:using hidden fields in express js在 express js 中使用隐藏字段
【发布时间】:2016-12-21 13:12:42
【问题描述】:

我正在尝试传递隐藏 id 字段的值并使用它从 mongodb 中检索记录并将其显示在配置文件页面上,点击索引页面上的“阅读更多”后。这是 index.ejs:

     <% for(i=0; i<users.length; i++){%>
        <div class="col-lg-3 center">
            <div class="text-center">
                <img class="img img-circle" src="<%= users[i].image %>" height="120px" width="120px" alt=""> <br>
                <h4><b><%= users[i].fname %> <%= users[i].lname %></b></h4>
                <ul class="list-inline social-buttons">
                <li><a href="<%= users[i].linkedin %>"><i class="fa fa-linkedin"></i></a></li>
                <li><a href="<%= users[i].gitHub %>"><i class="fa fa-github"></i></a></li>
                <li><a href="#"><i class="fa fa-twitter"></i></a></li>
                </ul>
                <input type="hidden" name="id" value="<%= users[i]._id %>" >
                <p><%=users[i].bio %>....<a href="prof">Read More</a></p>

            </div>
        </div><!-- col-lg-3 -->
        <% } %>

这里是 profile.ejs:

 <div class="medium-4 small-12 columns">
    <h3> <%= users.fname %>  <%= users.lname %></h3>
    <p>Username: <%= users.username %></p>
    <p>Email: <%= users.email %></p>
    <p> Software Developer at <%= users.role %></p>
  </div>

和快递路线,users.js。

  app.get('/prof',function(req, res) {
     var id=req.body.id;
     var user = new User();
    mongoose.model('User').findById(id,function(err, users){
       console.log(users);
        res.render('pages/profile',{users:users});
     });
  });

这给了我一个错误“无法读取 null 的属性'用户名'..”

我错过了什么?

【问题讨论】:

  • 你可以试试users[i].bio.username 吗?如果我没记错users[i].bio 为空。
  • "Cannot read property 'username' of null.." 告诉我们 mongod 没有返回结果。尝试控制台隐藏的发布值是什么结果。
  • 索引页面正在运行,users[i].bio 不为空。只有当我点击“阅读更多”时,我才会得到错误“无法读取 null 的属性'用户名'..”
  • 它返回 undefine @owaishanif786

标签: javascript mongodb express ejs


【解决方案1】:

您正在执行GET 操作,但您正尝试通过使用req.body.variable_name 获取的POST 参数发送id 值。

在这种情况下,您不需要隐藏字段,因为它需要 POST 操作才能发送到服务器。尝试将 id 作为 URL 参数发送,例如可以使用 req.param.id 或 req.query.id 获取该参数

http://example.com/api/users?id=4&token=sdfa3&geo=us

或

http://example.com/api/users/4/sdfa3/us

如果要获取查询参数?id=57ada56845b8466147fc35b0,则使用req.query

网址:

// GET /prof?id=57ada56845b8466147fc35b0

标记:

<p><%=users[i].bio %>....<a href="prof?id=<%= users[i]._id %>">Read More</a></p>

路线:

app.get('/prof', function(req, res) {
    var id = req.query.id; // 57ada56845b8466147fc35b0
    mongoose.model('User').findById(id, function(err, user){
        console.log(user);
        res.render('pages/profile', { users: user });
    });
});

其他参数使用

网址:

// GET /prof/57ada56845b8466147fc35b0

使用req.params.id

标记:

<p><%=users[i].bio %>....<a href="prof/<%= users[i]._id %>">Read More</a></p>

路线:

app.get('/prof/:id', function(req, res) {
    var id = req.params.id; //57ada56845b8466147fc35b0
    mongoose.model('User').findById(id, function(err, user){
        console.log(user);
        res.render('pages/profile', { users: user });
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多