Meteor-files: Exception in delivering result of invoking '_FilesCollectionWrite_assets': TypeError: Cannot read property 'link' of undefined

Created on 14 Oct 2017  路  3Comments  路  Source: veliovgroup/Meteor-Files

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.

question

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.

All 3 comments

@tobitech ,

  1. Before you can query FilesCollection you have to publish, and subscribe on its data. As with any other Meteor's collection. That's why you're getting undefined
  2. __Please read the docs before asking the question here__, as it has the answer on your question. Taking a look on the .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:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Cavain picture Cavain  路  4Comments

schlaegerz picture schlaegerz  路  3Comments

nayayan picture nayayan  路  3Comments

stefanve picture stefanve  路  4Comments

menelike picture menelike  路  3Comments