Meteor 在iron路由器中设置属于用户的数据

Meteor 在iron路由器中设置属于用户的数据,meteor,iron-router,Meteor,Iron Router,它在浏览器中显示错误: 未捕获的TypeError:无法读取未定义的属性“\u id” 关于如何获取已登录用户的设置记录的任何建议?Meteor登录过程通常需要几毫秒才能准备就绪,同时Meteor.user()将返回未定义的,并且第一次执行路由数据方法将失败 您可以使用Meteor.userId()避免这种情况发生,直到用户真正连接起来 Router.route('/settings', { name: 'settings', data: function() { return

它在浏览器中显示错误:

未捕获的TypeError:无法读取未定义的属性“\u id”



关于如何获取已登录用户的设置记录的任何建议?

Meteor登录过程通常需要几毫秒才能准备就绪,同时
Meteor.user()
将返回
未定义的
,并且第一次执行路由
数据
方法将失败

您可以使用
Meteor.userId()
避免这种情况发生,直到用户真正连接起来

Router.route('/settings', {
  name: 'settings',
  data: function() {
    return Settings.findOne({userId: Meteor.user()._id});
  }
});

您好,这很有效…但是您能解释一下为什么要在生效之前添加Meteor.user()&&吗???第一次运行是否将userId设置为未定义,并且将继续运行Settings.findOne,但只查找userId:“未定义”??最后,一旦发生反应,它将以正确的值重新运行??准确地说,但只需使用
Meteor.userId
Router.route('/settings', {
  name: 'settings',
  data: function() {
    return Settings.findOne({
      userId: Meteor.userId()
    });
  }
});