Retrieving Data from Meteor Collections

前端 未结 5 1333
北恋
北恋 2021-02-08 18:54

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 successful

相关标签:
5条回答
  • 2021-02-08 19:20

    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');
    
    0 讨论(0)
  • 2021-02-08 19:24

    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.

    0 讨论(0)
  • 2021-02-08 19:26

    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.

    0 讨论(0)
  • 2021-02-08 19:32

    try this way

    Meteor.subscribe('testData', function() {
      var document = Documents.find();
      console.log(document); 
    });
    
    0 讨论(0)
  • 2021-02-08 19:42

    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
    
    0 讨论(0)
提交回复
热议问题