首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >铁:路由器+流星-不能同时将POST数据添加到数据库和呈现路由

铁:路由器+流星-不能同时将POST数据添加到数据库和呈现路由
EN

Stack Overflow用户
提问于 2015-10-18 15:36:58
回答 2查看 106关注 0票数 1

我正在用Meteor编写一个应用程序,它需要从POST请求中获取数据,并在同一条路径上生成一个成功的页面。这是我当前用于/submit路由的代码:

代码语言:javascript
运行
复制
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'}时,它将呈现模板,但不会将数据添加到数据库中。

如何获得要添加的数据和要呈现的模板?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-18 19:56:42

问题是,要将数据发送到路由,必须在服务器上运行,并且不能从服务器路由呈现客户端模板。解决此问题的一种方法是使用302重定向返回客户端,如下所示(代码为coffeescript):

代码语言:javascript
运行
复制
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路由的名称用于标识要呈现的模板。

票数 3
EN

Stack Overflow用户

发布于 2015-10-18 15:57:41

isClientisServer之外试一试

代码语言:javascript
运行
复制
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()
    });
   }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33199817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档