Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 我在哪里设置会话默认值,以便它们在我的订阅中可用?_Meteor - Fatal编程技术网

Meteor 我在哪里设置会话默认值,以便它们在我的订阅中可用?

Meteor 我在哪里设置会话默认值,以便它们在我的订阅中可用?,meteor,Meteor,我有一个helper函数,它依赖于集合文档查找,其结果通过会话传递给订阅。然后需要从该订阅中查询文档 代码比我解释得更好 助手: var selection = Selections.findOne() var itemIds = function() { return selection && selection.itemIds } var itemIdsArray = itemIds() Session.set('itemIdsArray', itemIdsA

我有一个helper函数,它依赖于集合文档查找,其结果通过会话传递给订阅。然后需要从该订阅中查询文档

代码比我解释得更好

助手

var selection = Selections.findOne() 

var itemIds = function() {
    return selection && selection.itemIds
}

var itemIdsArray = itemIds()

Session.set('itemIdsArray', itemIdsArray)
console.log(Session.get('itemIdsArray'))

_.each(Items.find({_id: {$in: itemIdsArray}}).fetch(), function(element, index, list) {
    //doing stuff
})
Meteor.subscribe('itemsById', Session.get('itemIdsArray'))
Meteor.publish('itemsById', function(itemIdsArray) {
    return Items.find({_id: {$in: itemIdsArray}})
})
订阅

var selection = Selections.findOne() 

var itemIds = function() {
    return selection && selection.itemIds
}

var itemIdsArray = itemIds()

Session.set('itemIdsArray', itemIdsArray)
console.log(Session.get('itemIdsArray'))

_.each(Items.find({_id: {$in: itemIdsArray}}).fetch(), function(element, index, list) {
    //doing stuff
})
Meteor.subscribe('itemsById', Session.get('itemIdsArray'))
Meteor.publish('itemsById', function(itemIdsArray) {
    return Items.find({_id: {$in: itemIdsArray}})
})
出版物

var selection = Selections.findOne() 

var itemIds = function() {
    return selection && selection.itemIds
}

var itemIdsArray = itemIds()

Session.set('itemIdsArray', itemIdsArray)
console.log(Session.get('itemIdsArray'))

_.each(Items.find({_id: {$in: itemIdsArray}}).fetch(), function(element, index, list) {
    //doing stuff
})
Meteor.subscribe('itemsById', Session.get('itemIdsArray'))
Meteor.publish('itemsById', function(itemIdsArray) {
    return Items.find({_id: {$in: itemIdsArray}})
})
My
console.log
在返回ID数组之前返回一个
未定义的值。因此,
undefined
一路被传递到发布,发布抱怨
null
值(这本身很奇怪)在
$in
之后中断

我的解决方案是将会话设置为默认值
[]

Session.setDefault(`itemIdsArray`, [])
老实说,我对它能起作用寄予厚望,但可惜没有

我试着把它放在IronRouter的
onBeforeAction
,我试着把它放在助手的顶部,我试着把它放在几乎任何地方,但它仍然会记录并返回一次未定义的
值,直到它得到正确的值

我也尝试过在订阅中四处移动,从
waitOn
subscriptions
再到
onAfterAction
再到
onRendered
,但这些尝试都是徒劳的


我该怎么办?

如果您在Iron Router的waitOn选项中返回订阅,您的模板中应该有数据,然后:

Router.route('/yourRoutePath/:_id', {
  // this template will be rendered until the subscriptions are ready
  loadingTemplate: 'loading',

  waitOn: function () {
    // return one handle, a function, or an array
    return Meteor.subscribe('itemsById', this.params._id);
  },

  action: function () {
    this.render('myTemplate');
  }
});
您的模板助手:

Template.myTemplate.helpers({
  items: function() {
    return Items.find();
  }
});
我注意到您发布了Items集合,并且希望在助手中使用Selections集合。如果需要多个订阅,可以在waitOn中返回订阅数组:

waitOn: function () {
  // return one handle, a function, or an array
  return [
    Meteor.subscribe('itemsById', this.params._id),
    Meteor.subscribe('selections')
  ];
}

WaitOn确保在所有订阅就绪时呈现模板。

这是Meteor中相当典型的行为。会话变量此时并不总是准备就绪。处理这种情况的通常方法是在助手中引入一个保护,在对变量执行任何其他操作之前检查该变量是否已定义

在您的情况下,类似这样的方法会起作用:
itemsIdArray=itemIds()| |[]


要回答您提出的实际问题,您在哪里设置会话默认值,以确保它们在订阅中可用:在哪里设置它们并不重要,重要的是何时访问它们。您可以使用iron router的waitOn()函数等待订阅准备就绪,也可以在助手的“var itemIDSRARY=itemIds()”之后检查订阅句柄的ready()函数(请参见)

,尝试简单地输入“if(!itemIDSRARY)return;”。在Meteor中,这样一点防御性的编码通常就足够了。