Iron:Router + Meteor - 不能同时向数据库添加 POST 数据和渲染路由

Iron:Router + Meteor - Can't both add POST data to database and render route

我正在用 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'});

当我 运行 这段代码时,数据被插入到 Records 数据库中,但它从未呈现成功模板。当我转到 /submit 路由时,它只会永远加载,而不会在页面上实际显示任何内容。当我摆脱 {where: 'server'} 时,它将呈现模板但不会将数据添加到数据库中。

如何同时获取要添加的数据和要呈现的模板?

isClientisServer

之外试试这个
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()
    });
   }
});

问题是 POST 到路由的数据必须在服务器上 运行 并且您无法从服务器路由呈现客户端模板。解决这个问题的一种方法是使用 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 路由的名称用于标识要呈现的模板。