我目前正在尝试从特定路由规则内的表单中捕获发布的值。由于所有其他关于此的SO帖子都无法使用,因此我想再问一遍。您是否已在项目中整理并实现了此功能?是否有[email protected]的解决方案?
this.request.body
路由规则内的以上代码始终返回未定义。
Router.route('/register', function(){
console.log( JSON.stringify(this.request.body) );
//this.render('test',{data:{data:this.request.body.username}})
});
//SERVER ONLY
if (Meteor.isServer) {
Meteor.methods({
'addSong': function(songName) {
var userId = Meteor.userId()
songs.insert({
userId: userId,
name: songName
})
}
})
Router.onBeforeAction(Iron.Router.bodyParser.urlencoded({
extended: true
}));
}
[铁路由器指南lets us know,this.request
和this.response
是“ NodeJS请求和响应对象”。
如果查看req.body
的某些文档,您会发现:
默认情况下,它是未定义的,使用时会填充人体分析中间件,例如人体分析器和multer。
来自req.body
:
IR在Iron.Router.bodyParser上提供express的正文分析器。
所以就在那里!如果要填充Iron-router's guide,则应添加:
this.request.body
我为控制器创建了一个文件,以在控制器文件夹下维护代码可重用性名称为solar.js,即solar文件具有我的数据库功能,并将请求和响应作为该文件的参数传递,例如export.getSolarInfo =(req, res)=> {console.log(req.body)},在这里您会得到自己的参数。然后在此处操作我们的功能,然后发送响应,例如response = {“ status”:0,“ result”:“ invalid query”} res.end(JSON.stringify(response));
//导入控制器const SOLAR = require('./ controllers / solar.js');
Router.onBeforeAction(Iron.Router.bodyParser.urlencoded({
extended: true
}));