0

我正在使用 node.js express 和 mongodb,我有一个来自客户端的输入数据,我需要将数据传递给服务器查找它的属性并在另一个页面中发送给客户端。

现在我有问题, req.body.age应该从客户的输入中获取数据并用于find()获取其适当的属性。

服务器端代码:

函数在另一个 .js 文件中路由

exports.find_user = function(req, res) {
    res.render('find_user.jade');
};


exports.user = function(req, res){
    member = new memberModel();
    member.desc.age = req.body.age; //problem
    console.log(req.body.age);  //undefined 
    memberModel.find({desc: {age: '7'}}, function(err, docs){
        res.render('user.jade', { members: docs });
    console.log(docs);
});
};

memberModel.find({desc: {age: '7'}}只需硬编码 7 岁的用户(有效)

客户端代码(玉):

数据输入页面:

find_user.jade

form(action='/', method='post')
    fieldset
        lable(for="age") Find user by age: 
            input(type="text", size="30", name="age", required="required")
            input(type='button', value='Find', onclick='location.href=\'find_user/user/\'')

数据输出页面及其属性:

用户.jade

        tr
            th Name
            th Age
    tbody
    - members.forEach(function(member){
        tr
            td= member['name']
            td= member['desc']
     - });
4

1 回答 1

0

You are not submitting your data in find_user.jade file when the user clicks the button. Instead, the client is only redirected to another page.

This is how your find_user.jade file should look like:

form(action='find_user/user/', method='post')
    fieldset
        label(for="age") Find user by age: 
            input(type="text", size="30", name="age", required="required")
            input(type='submit', value='Find', name="submit")
于 2012-08-03T11:17:36.310 回答