流星从出版物返回空光标

问题描述 投票:1回答:1

我的出版物前面有一些中间件:

 Meteor.publish(publicationIdentifier, function (...args) {
   try {
     middleware()
   } catch(error) {
     return Users.find('emptyCursor')
   }
   return Model.pubsub(...args)
 })

当中间件抛出错误时,我需要返回一个空的Cursor。我目前通过在某个任意集合上使用带有无效id的find来执行此操作:return Users.find('emptyCursor')

有没有更好的方法来做到这一点?

我试过了

return 
return false
return null
return new Mongo.Cursor()
javascript meteor
1个回答
1
投票

就像在doc

    // Sometimes publish a query, sometimes publish nothing.
Meteor.publish('secretData', function () {
  if (this.userId === 'superuser') {
    return SecretData.find();
  } else {
    // Declare that no data is being published. If you leave this line out,
    // Meteor will never consider the subscription ready because it thinks
    // you're using the `added/changed/removed` interface where you have to
    // explicitly call `this.ready`.
    return [];
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.