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.<anonymous> (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传出来?
var myname = searchResult.doSearch(req,res);
你REQ没丢进去。req在INDEX.JS里面就是UD
@MiguelValentine 谢谢!但是我试了还是不起作用!
亲,缺少参数,前面导出的方法带有req,res,而你的body属性是req的,这里引用的时候不带参数当然就是找不到body from undefined
首先像一楼说的 要var myname = serarchResult.doSearch(req, res) 试了还不行要看是不是出错了报什么错, 你可以在search.js中输出req 看下里面的req,body的信息是否有username