Retrieving Data from Meteor Collections

旧巷老猫 提交于 2020-02-18 14:28:06

问题


I'm having a few problems when trying to get data from a Meteor Collection and I need some advice.

The collection has been defined, published, and subscribed successfully. If I send the data to a template, it displays fine:

Template.Lists.Projects = function(){
    return Projects.find();
};

But I am trying to use the data before displaying it, and this is where I run into problems. First, I'm getting some inconsistencies between find() and findOne(). find(selector) works fine and returns a cursor, but findOne(selector) returns "undefined." I really am only looking for 1 entry, so find() seems unnecessary.

Returns LocalCollection.Cursor:

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
console.log(find);

Returns undefined:

var find = Projects.findOne({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
console.log(find);

My next problem arises when using .fetch() on the LocalCollection.Cursor. It returns an empty array.

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
var fetch = find.fetch();
console.log(fetch);

all this returns is the following line:

[ ]

When I try to specify a specific key from the array I want to display, like:

var find = Projects.find({_id: "3fd33eed-9735-4376-860e-3be383beae2f"});
var fetch = find.fetch();
console.log(fetch.name);

It returns undefined.

I'm still familiarizing myself with Meteor and have never used MongoDB (or minimongo), so I'm probably just making some stupid mistake. If anyone can point it out to me I would be thrilled!


回答1:


Your results for find() and findOne() are consistent. Basically, Mongo or minimongo is simply not finding a document that matches that _id. FindOne() is precisely like doing a find(selector, options).fetch()[0].

Your Lists.Projects template is likely expecting a collection, array or hash it can iterate over. You cannot return one specific document. If you are using {{#each Projects}} you must provide some way for the template to iterate not just a single value.




回答2:


I recently had the same problem, The collection find() returned nothing when used from a query.observe.

The problem was the order of the subscribe of collections.

For example if you have a collection called Lists and one called Projects,

If you're getting the projects by observing a query on lists, and you had :

Meteor.subscribe('Lists');
Meteor.subscribe('Projects');  

What happens is the query observe trigger is called, but the Projects are not yet fetched from the server. So Projects.find().fetch().length = 0.

To fix it, simply do

Meteor.subscribe('Projects');    
Meteor.subscribe('Lists');



回答3:


if u've removed autopublish, what if u publish the collection to all user without using a publish name?

Meteor.publish null, ->
    Products.find {}

where u subscribe your collection ?

template helper

Handlebars.registerHelp = 'products', (id) ->
    Product.find _id: Session.get 'productId'

like if we have price in each product. the template part looks like...

<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>

the js part, i'll use 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



回答4:


try this way

Meteor.subscribe('testData', function() {
  var document = Documents.find();
  console.log(document); 
});



回答5:


You are working on the client and you never know when the client got all the data you need. Your functions can be fired when the collections are empty or still not finished synchronized. So you have to make a deferred request to your minimongo (when all data is available local)

And yes you can´t access stuff when its not rendered in the DOM via getElementById() or something but in your case you try to access data from the minimongo (your local mongodb version in the browser) not the DOM so your template is not important here.

Just wait until your subscribtion is ready that your minimongo own all the data with the onReady callback in your subscribtion call and fire your functions.

https://docs.meteor.com/api/pubsub.html#Meteor-subscribe

callbacks (Function or Object).

Optional. May include onStop and onReady callbacks. If there is an error, it is passed as an argument to onStop. If a function is passed instead of an object, it is interpreted as an onReady callback.



来源:https://stackoverflow.com/questions/13940807/retrieving-data-from-meteor-collections

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!