当我尝试使用find()
时如下:
Meteor.publish('currentRequest', function (requestId) {
console.log(requestId)
console.log( Requests.find( {_id: new Meteor.Collection.ObjectID(requestId)} ).fetch() ); // The item is printed successfully to the terminal
return Requests.find( {_id: new Meteor.Collection.ObjectID(requestId)} ).fetch();
});
这是我在客户端中订阅的方式:
Template.requestView.onCreated(function () {
var self = this;
self.autorun(function() {
self.subscribe('currentRequest', Session.get('requestId'));
});
});
Ane是我的 helper :
Template.requestView.helpers({
currentRequest: function() {
console.log(Requests.findOne(new Meteor.Collection.ObjectID(Session.get('requestId'))) );
return Requests.find( {_id: new Meteor.Collection.ObjectID(requestId)} ).fetch();
}
});
但我收到以下错误:
Exception from sub currentRequest id m9X5fgYNNtLN6JAXt Error: Publish function returned an array of non-Cursors
当我尝试从 find()
更改上面的所有代码时至findOne()
如下
Requests.findOne(new Meteor.Collection.ObjectID(requestId));
我收到以下错误:
Exception from sub currentRequest id vqjwt7sggLESxeNtc Error: Publish function can only return a Cursor or an array of Cursors
最佳答案
删除fetch()
。
这会将光标从 find()
转换为数组,Meteor.Publish
不喜欢这种数组,并在错误消息中提示。
关于javascript - 如何将集合中的一份文档发布到客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46285431/