在服务器和客户端之间传递数据(node.js + mongodb) [英] passing data between server and client (node.js + mongodb)
问题描述
我正在使用node.js express和mongodb,我有来自客户端的输入数据,我需要将数据传递给服务器查找其属性并发送到另一页面的客户端。
I'm working with node.js express and mongodb, I have a input data from client, I need to pass the data to server look for its property and send to the client in another page.
现在我遇到了 req.body.age
的问题,它假定从客户端输入获取数据并使用 find ()
获取相应的属性。
Now I have problem with req.body.age
that suppossed to get the data from client's input and use find()
to get its appropriate property.
服务器端代码:
函数在另一个.js文件中路由
functions are routed in another .js file
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岁以上的用户(工作)
memberModel.find({desc: {age: '7'}}
just hardcode picking up user with age 7 (works)
客户端代码(jade):
数据输入页面:
find_user.jade
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/\'')
数据输出页面及其属性:
page for data output with its property:
user.jade
user.jade
tr
th Name
th Age
tbody
- members.forEach(function(member){
tr
td= member['name']
td= member['desc']
- });
推荐答案
当用户单击按钮时,您没有在find_user.jade文件中提交数据。相反,客户端只被重定向到另一个页面。
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.
这是你的find_user.jade文件的样子:
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")
这篇关于在服务器和客户端之间传递数据(node.js + mongodb)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!