我在尝试从Meteor Collection获取数据时遇到了一些问题,我需要一些建议.
该集合已成功定义,发布和订阅.如果我将数据发送到模板,它显示正常:
Template.Lists.Projects = function(){
return Projects.find();
};
Run Code Online (Sandbox Code Playgroud)
但我试图在显示之前使用数据,这就是我遇到问题的地方.首先,我在find()和findOne()之间遇到了一些不一致.find(selector)工作正常并返回游标,但findOne(selector)返回"undefined".我真的只想找一个条目,所以find()似乎没必要.
返回LocalCollection.Cursor:
var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
console.log(find);
Run Code Online (Sandbox Code Playgroud)
返回undefined:
var find = Projects.findOne({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
console.log(find);
Run Code Online (Sandbox Code Playgroud)
在LocalCollection.Cursor上使用.fetch()时出现了下一个问题.它返回一个空数组.
var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
var fetch = find.fetch();
console.log(fetch);
Run Code Online (Sandbox Code Playgroud)
所有这些返回的是以下行:
[]
当我尝试从我想要显示的数组中指定特定键时,例如:
var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
var fetch = find.fetch();
console.log(fetch.name);
Run Code Online (Sandbox Code Playgroud)
它返回undefined.
我还在熟悉Meteor并且从未使用过MongoDB(或者还有Minorongo),所以我可能只是犯了一些愚蠢的错误.如果有人能指出我,我会很激动!
如果您已删除自动发布,如果您在不使用发布名称的情况下将集合发布给所有用户怎么办?
Meteor.publish null, ->
Products.find {}
Run Code Online (Sandbox Code Playgroud)
你在哪里订阅你的收藏?
模板助手
Handlebars.registerHelp = 'products', (id) ->
Product.find _id: Session.get 'productId'
Run Code Online (Sandbox Code Playgroud)
就像我们每种产品都有价格一样。模板部分看起来像......
<template name="products-list">
<div class="products-list">
{{#each products}}
{{> product-item}}
{{/each}}
</div>
</template>
<template name="product-item">
<div class="product-item">
{{price}}
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
js部分,我将使用coffeescript...
Template['product-item'].price = ->
console.log @ # @ is this as it is product itself, if we have product inserted.
console.log this
return 'the price is: ' + @price
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14054 次 |
| 最近记录: |