I am encountering an issue with displaying a file record in my template. The only thing that is being returned to the template is the 'cursor' object.
Here is the console.log(file) output from the JS console:
FilesCollection {collectionName: "Files", downloadRoute: "/cdn/storage", schema: Object, chunkSize: 524288, namingFunction: false…}
I have gone through various posts and resources but it seems that it returns a cursor to the client. However, I am running a Files.findOne() query which ideally should return the record itself to the template or HTML.
'/imports/api/files.js'
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
export const Files = new FilesCollection({
collectionName: 'Files',
allowClientCode: false,
onBeforeUpload: function (file) {
if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
return true;
} else {
return 'Please upload image, with size equal or less than 10MB';
}
}
});
if (Meteor.isServer) {
Meteor.publish('getFile', function(id) {
return Files.find({_id: id }).cursor;
});
}
'/imports/ui/components/download.js'
import './download.html';
import { Files } from '../../api/files.js';
Template.download.onCreated(function() {
let self = this;
self.autorun(function() {
let fileId = FlowRouter.getParam('id');
self.subscribe('getFile', fileId);
});
});
Template.download.helpers({
file: function () {
let fileId = FlowRouter.getParam('id');
let file = Files.findOne({_id: fileId}) || {};
console.log(file)
return file;
}
});
'/imports/ui/components/download.html'
<template name='download'>
{{#if Template.subscriptionsReady}}
{{#with file}}
<a href="{{link}}?download=true" download="{{name}}" target="_parent">
{{name}}
</a>
<p>{{link}}</p>
<h1>subscriptions are ready!</h1>
<h2>{{collectionName}}</h2>
{{/with}}
{{else}}
<p>Loading...</p>
{{/if}}
</template>