流星发现没有返回数据

时间:2015-05-19 20:03:11

标签: javascript mongodb meteor iron-router

我的收藏是在collections.js

中创建的
portfolioCategories = new Mongo.Collection('portfoliocategories');

然后订阅subscriptions.js

Meteor.subscribe('portfoliocategories');

并在publications.js上发表

Meteor.publish('portfoliocategories',function(){
    return portfolioCategories.find();
});

如果我使用 db.portfoliocategories.find()从服务器查询Mongo,我会得到

  

{" _id" :" W9AeauCpMgPw2j5hf"," title" :"商业设计"," slug" :"商业设计","图像" :" https://url/9c3ba46d-c09a-4a7d-ac40-5752fc24ad2c.jpg" }

然而,从客户和铁路两者中,找不回报。例如,如果我输入控制台:

portfolioCategories.find({'_id':'W9AeauCpMgPw2j5hf'});

我将获得一个带有未定义键值的LocalCollection.Cursor:

  

_selectorId:undefined

     

_transform:null

     

collection:LocalCollection

     

字段:未定义

     

限制:未定义

如果我尝试将其作为数据返回,那么在铁路由器中也会发生同样的事情。然而,如果我使用findOne,我将获得该文件。

portfolioCategories.findOne({'_id':'W9AeauCpMgPw2j5hf'})
  

对象{" _id" :" W9AeauCpMgPw2j5hf"," title" :"商业设计"," slug" :"商业设计","图像" :" https://url/9c3ba46d-c09a-4a7d-ac40-5752fc24ad2c.jpg" }

我的问题是我需要返回所有具有相同标题的项目。因此,findOne()不是一个合适的解决方案。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

由于铁路由器的数据对象之间存在差异,因此出现了混乱。我试图用铁路由器做的是

waitOn:function(){

    return [Meteor.subscribe('projectsportfolio'),Meteor.subscribe('portfoliocategories')];

}
data:function(){
    currentSlug = this.params.category;
    currentCategory = projectsPortfolio.find({slug:currentSlug});
    if(typeof currentCategory != 'undefined'){
        return currentCategory
    }
}

但这只会返回@David Weldon解释的光标。 解决方案是:

data:function(){
    currentSlug = this.params.category;
    currentCategory = projectsPortfolio.find({slug:currentSlug}).fetch();
    if(typeof currentCategory != 'undefined'){
        return currentCategory
    }
}

currentCategory现在将是一个对象数组,我现在可以在我的模板中循环

{{#each currentCategory}}
    {{keyvalue}}
{{/each}}
相关问题