Sang Yoo Kim
Sang Yoo Kim

Reputation: 365

Return arrays from meteor mongo

ajduke:bootstrap-tagsinput

I am using the above package to create a tags system. I have used <select multiple> from the True Input Value from the link above and have inserted each tags as Strings within the tag array.

This is what my db looks like.

Posts
    tags              //Array
        [0] : sometag //String
        [1] : sometag //String
        [2] : sometag //String
                      //and so forth..

So my question is... I want to return all the tags within a post to the following format, so that I can reuse it to show the tags when my users try to edit their posts.

Wanted Format

['sometag', 'sometag', 'sometag', and so forth]

Edit1

this is what I've done so far.

Post_Edit.js

Template.postEdit.rendered = function() {
    myTags = Posts.findOne({_id: this._id}).tags.fetch(); //AAA
    
    $('.tagsinput').tagsinput('add', myTags); //From above link API
}

I've tried other methods for //AAA line but I am having no luck.I've tried things such as Posts.findOne.map... Maybe my english comprehension is not up to par but the documentations in meteor did not help me understand any better.

Edit 2

Posts.findOne({_id: "ziZw3wLaxFyz3DYP4"}).tags I've tried putting this in the browser console and got the array in the format that I wanted.

But the problem is, it won't work in my app when I use it.

When I use Posts.findOne({_id: this._id}).tags in my postEdit.rendered, I get this browser console error. Cannot read property 'tags' of undefined Post_Edit.js

Template.postEdit.rendered = function() {
    myTags = Posts.findOne({_id: this._id}).tags;

    
    $('.tagsinput').tagsinput('add', myTags); //From above link API
}

What I don't understand is why is it working in browser console but not my Template.postEdit.rendered?

Upvotes: 0

Views: 1109

Answers (2)

wuguay
wuguay

Reputation: 11

You are correct that your variable myTags is undefined when Template.postEdit.rendered is called because the Posts database has not finished loading when your function is called.

myTags = Posts.findOne({_id: this._id}).tags.fetch(); //AAA

Solution
There are multiple strategies including:

  1. Updating DOM after subscription completes
    http://docs.meteor.com/#/full/Blaze-TemplateInstance-subscribe

  2. Re-run the function when Collection changes
    http://docs.meteor.com/#/full/tracker_autorun

    Template.postEdit.rendered = function() {
       Tracker.autorun(function () {  ** NEW LINE **
          myTags = Posts.findOne({_id: this._id}).tags; 
    
          $('.tagsinput').tagsinput('add', myTags); 
       } ** NEW LINE **
    }
    

Upvotes: 1

Sang Yoo Kim
Sang Yoo Kim

Reputation: 365

I've approached a completely different way to figure this out. But I'd still like to know why my method did not work...

post_edit.js

Template.postEdit.helpers({
    tags: function(){
        Posts.findOne({_id:this._id}).tags;
    }
});

post_edit.html

<select class=tagsinput id="tagsinput" data-role="tagsinput">
    {{#each}}
        <option value="{{this}}">{{this}}</option>
    {{/each}}

Upvotes: 0

Related Questions