0

I have defined the following Collection in my application:

Projects = new Mongo.Collection("projects")

In this Collection, I have inserted:

Projects.insert({
    source: "https://upload.wikimedia.org/wikipedia/commons/7/7f/Pug_portrait.jpg",
    title: "pug",
    artist: "pug",
    description: "This piece shows the duality of pug",
    price: "priceless"
});

Projects.insert({
    source: "https://i.sstatic.net/D2ABD.gif",
    title: "doge",
    artist: "doge",
    description: "much doge, many deal with it, wow",
    price: "bout tree fiddy"
})

I am attempting to create an array of the sources of the images using the following helper function:

sourceArray : function () {
          // returns array of sources
          var sources = [];
          for (var i = 0; i < Projects.find().count(); i++) {
            sources.push(images[i].source);
          }
          return sources;

      }

the "images variable is previously defined as: images = Projects.find().fetch();

I then call the helper function in my HTML.

<p>{{sourceArray}}</p>

On the page, the first source appears fleetingly, but disappears within a few seconds. In the browser console the following is shown:

meteor.js:888 Exception in template helper: TypeError: Cannot read property 'source' of undefined
    at Object.Template.body.helpers.sourceArray (http://localhost:3000/art.js?913b8578eb54cde21abc07c994f6b29267232bc5:61:28)
    at http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2880:16
    at http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:1651:16
    at http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2928:66
    at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:3476:12)
    at http://localhost:3000/packages/blaze.js?a5c324925e5f6e800a4c618d71caf2848b53bf51:2927:27
    at Spacebars.call (http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:172:18)
    at Spacebars.mustacheImpl (http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:109:25)
    at Object.Spacebars.mustache (http://localhost:3000/packages/spacebars.js?7bafbe05ec09b6bbb6a3b276537e4995ab298a2f:113:39)
    at null._render (http://localhost:3000/template.art.js?30aa5e2d0b6d3de2f69b7341296ae51b8ce737ba:27:22)

The exception refers to this line of code:

sources.push(images[i].source);

How can this be fixed?

1
  • I suspect that you are not publishing and subscribing to the Projects collection. As you create the projects they temporarily exist on the client but are then removed. Commented Jul 21, 2015 at 20:21

2 Answers 2

1

Try:

sourceArray : function () {
      var images = Projects.find().fetch();

      var sources = [];
      for (var i = 0; i < images.length; i++) {
        sources.push(images[i].source);
      }
      return sources;

  }

If as you said, you defined images as a template helper property, you may need

this.images[i].source
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure I understand what you mean.
1

Just redefine it in your helper. That's probably what you want to anyway so it will be reactive. Here's an implementation that should work:

sourceArray: function() {
  return _.pluck(Projects.find().fetch(), 'source');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.