javascript - 如何将集合中的一份文档发布到客户端?

标签 javascript node.js meteor

当我尝试使用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/

相关文章:

javascript - 如何从字符串中删除所有出现的任何给定字符?

javascript 更改事件行为

javascript - 我如何在没有库的情况下构建 NodeJS Websocket 服务器

node.js - 在带有 iisnode 的虚拟目录中运行的 Node.js Express 3 应用程序的 socket.io 路由问题

node.js - 无法在node.js中发出请求

angularjs - 登录 AngularJS Meteor 后重定向

Meteor.publish 不是一个函数

meteor - 有条件地加载 Meteor 中的内部 Assets

javascript - 单击按钮一次后禁用按钮

javascript - 学生搜索项目 jquery/javascript