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?