【问题标题】:getting some part of URL获取 URL 的一部分
【发布时间】:2016-03-10 22:22:58
【问题描述】:

我正在尝试获取此 URL 的结尾部分(在服务器端)

http://localhost:3000/insAds/D79htZY8DQ3YmcscE

我的意思是我想得到这个字符串:

D79htZY8DQ3YmcscE

有一个类似的问题:How to get the query parameters in Iron-router?

但没有一个答案对我有帮助!因为我在 URL 中没有查询参数。

我知道这些代码给了我想要的字符串:

this.params.id

Router.current().params.id

但这些代码仅在客户端有效!我想在服务器端获取那个字符串!

最后我尝试获取该字符串并在此处使用:

Ads.before.insert(function(userId, doc) {
    //console.log(this.params.id);
    doc._categoryId = this.params.id;
    doc.createdAt = new Date();
    doc.createdBy = Meteor.userId();
});

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    你可以像这样使用Router.current().paramsthis.params

    Router.route('/insAds/:id', function () {
        console.log(this.params.id); // this should log D79htZY8DQ3YmcscE in console
    });
    

    查看iron router documentation快速入门部分的第三个示例

    编辑:根据我们的聊天记录,

    你的钩子是

    Ads.before.insert(function(userId, doc) {
        //console.log(this.params.id);
        doc._categoryId = this.params.id;
        doc.createdAt = new Date();
        doc.createdBy = Meteor.userId();
    });
    

    改成

    Ads.before.insert(function(userId, doc) {
        doc.createdAt = new Date();
        doc.createdBy = Meteor.userId();
    });
    

    然后像这样在服务器中定义meteor方法

    Meteor.methods({
        'myInsertMethod': function (id) {
             Ads.insert({
                 _categoryId: id
             });
        }
    });
    

    像这样从客户端调用它

    Meteor.call('myInsertMethod', Router.params().id, function (err, res) { 
        console.log (err, res);
    });
    

    【讨论】:

    • 我想在服务器端获取该字符串。 Router.current().params 仅在客户端有效。我对你的回答有这个错误:没有方法'当前'
    • @Roohollah 你能给我们一个示例代码或上下文,以便我可以建议以不同的方式解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2011-11-15
    • 2013-11-01
    • 2011-10-20
    • 2013-06-15
    相关资源
    最近更新 更多