CNode

node.js express 表单内容传出

Kkingche发布于12 年前最后回复12 年前5 回复6643 浏览0 收藏

1.我在routes/index.js中添加了提取表单内容 exports.doSearch = function(req, res){
//console.log(req.params.doSearch);//输出index var namevalue = req.body.username;
console.log(namevalue);//输出表单post提交的login_name
res.send('great you are right for post method:' + namevalue);//显示页面文字信息
return namevalue; }; 2.我在另外一个文件search.js文件中想引用index.js中的namevalue值 var searchResult = require('./routes/index'); var myname = searchResult.doSearch(); 发现报错: C:\project\ex03\routes\index.js:21 var namevalue = req.body.username; ^ TypeError: Cannot read property 'body' of undefined at Object.exports.doSearch (C:\project\ex03\routes\index.js:21:21) at Object. (C:\project\ex03\search.js:8:28) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3 3.请问我怎样才能将namevalue传出来?

查看回复

回复 (5)

M
MiguelValentine#1·12 年前

var myname = searchResult.doSearch(req,res);

M
MiguelValentine#2·12 年前

你REQ没丢进去。req在INDEX.JS里面就是UD

K
kingche#3·12 年前
引用 MiguelValentinevar myname = searchResult.doSearch(req,res);

@MiguelValentine 谢谢!但是我试了还是不起作用!

H
HarrisFeng#4·12 年前

亲,缺少参数,前面导出的方法带有req,res,而你的body属性是req的,这里引用的时候不带参数当然就是找不到body from undefined

H
Hi-Rube#5·12 年前

首先像一楼说的 要var myname = serarchResult.doSearch(req, res) 试了还不行要看是不是出错了报什么错, 你可以在search.js中输出req 看下里面的req,body的信息是否有username

参与回复
登录后即可参与回复。登录