我正在用Meteor编写一个应用程序,它需要从POST请求中获取数据,并在同一条路径上生成一个成功的页面。这是我当前用于/submit路由的代码:
Router.route('/submit', function() {
Records.insert({
testValue: 'The Value',
importantVal: this.request.body.email,
createdAt: new Date()
});
this.render('success');
}, {where: 'server'});当我运行这段代码时,数据会插入到记录数据库中,但它从不呈现成功模板。当我进入/submit路由时,它只会永远加载,而不会在页面上显示任何内容。当我摆脱{where:'server'}时,它将呈现模板,但不会将数据添加到数据库中。
如何获得要添加的数据和要呈现的模板?
发布于 2015-10-18 19:56:42
问题是,要将数据发送到路由,必须在服务器上运行,并且不能从服务器路由呈现客户端模板。解决此问题的一种方法是使用302重定向返回客户端,如下所示(代码为coffeescript):
Router.route '/submit', where: 'server'
.post ->
Records.insert
testValue: 'The Value'
importantVal: @request.body.email
createdAt: new Date()
@response.writeHead 302, 'Location': '/success'
@response.end()
Router.route '/success', name:'success'server路由接收POSTed数据并在重定向到client路由之前对其进行操作。client路由的名称用于标识要呈现的模板。
发布于 2015-10-18 15:57:41
在isClient和isServer之外试一试
Router.route('/submit', {
template: 'success',
onBeforeAction: function(){
Records.insert({
testValue: 'The Value',
importantVal: $('[name=email]').val(),//email from input field with name="email"
createdAt: new Date()
});
}
});https://stackoverflow.com/questions/33199817
复制相似问题