I'm trying to get a file from the collection and generate a download link for it, but I keep getting the error above. File uploaded successfully but can't retrieve it. Here is my code:
if (file) {
var uploadInstance = Assets.insert({
file: file,
streams: 'dynamic',
chunkSize: 'dynamic'
}, false);
uploadInstance.on('start', function() {
template.currentUpload.set(this);
});
uploadInstance.on('end', function(err, fileObj) {
if (err) {
FlashMessages.sendError(err.reason);
} else {
FlashMessages.sendSuccess('File "' + fileObj.name + '" successfully uploaded');
console.log(fileObj._id); // returns an Id
var fileRef = Assets.findOne({_id: fileObj._id});
console.log(fileRef); // returns undefined
var link = fileRef.link();
console.log(link);
}
template.currentUpload.set(false);
});
uploadInstance.start();
}
I don't know why it's returning undefined, but if I use Assets.find({_id: fileObj._id}) it returns a FilesCursor which I wasn't able to work with, throws an error that one must pass a plain object when I try to generate a link.
@tobitech ,
.link() method you may find out what it will work in the next way: uploadInstance.on('end', function(err, fileObj) {
if (err) {
FlashMessages.sendError(err.reason);
} else {
FlashMessages.sendSuccess('File "' + fileObj.name + '" successfully uploaded');
console.log(fileObj._id); // returns an Id
var link = Assets.link(fileObj);
console.log(link);
}
template.currentUpload.set(false);
});
I read the docs. And I already had Publications and Subscriptions setup for FilesCollection, .find() works but .findOne() didn't. I just didn't know why it was returning undefined. Anyway the .link() code you posted worked for me. Thank you.
Feel free to reopen it in case if the issue still persists on your end.
Please, support this project by:
Most helpful comment
I read the docs. And I already had Publications and Subscriptions setup for FilesCollection,
.find()works but.findOne()didn't. I just didn't know why it was returning undefined. Anyway the.link()code you posted worked for me. Thank you.