this._id流星中未定义

this._id undefined in Meteor

本文关键字:未定义 流星 id this      更新时间:2023-09-26

我正在尝试将我的selectedDocId设置为路由到页面,但流星抛出undefined。 我正在使用onBeforeAction来尝试存储通过 /:id 连接在我的 url 末尾的id,但我无法将它们对齐。

 Router.route('speaker', {
            path:'/speakers/:_id',
            template: 'speaker',
            data: function(){
                return Speakers.findOne(this.params._id);
            },
            onBeforeAction: function(){
                console.log('speaker route has run');
                Session.set('selectedDocId', this._id);
                //returns undefined
                console.log(this._id);
                this.next();
            }
        });
        Template.speaker.helpers({
            editingDoc: function(){
                return Speakers.findOne({_id: Session.get('selectedDocId')});
            }
        });

而不是this._id,使用 this.params._id 就像你在 data 钩子中所做的那样。