@@ -22,13 +22,32 @@ Template.afFileUpload.onCreated(function () {
2222 } ;
2323 }
2424
25- const mongoCollection = Mongo . Collection . get
26- ? Mongo . Collection . get ( this . data . atts . collection )
27- : Meteor . connection . _mongo_livedata_collections [ this . data . atts . collection ]
25+ // primary method: use dburles:mongo-collection-instances
26+ if ( Mongo . Collection . get ) {
27+ const mongoCollection = Mongo . Collection . get ( this . data . atts . collection ) ;
28+ this . collection = mongoCollection && mongoCollection . filesCollection ;
29+ }
30+
31+ // 1. fallback using global scope
32+ if ( ! this . collection ) {
33+ this . collection = global [ this . data . atts . collection ] ;
34+ }
35+
36+ // 2. fallback using Meteor.connection / local collections
37+ // if the Meteor release is newer than 2016 -> use _stores
38+ // else use older _mongo_livedata_collections
39+ // see https://github.com/meteor/meteor/pull/5845
40+ if ( ! this . collection ) {
41+ const storedCollection = Meteor . connection . _stores [ this . data . atts . collection ] ;
42+ this . collection = ( storedCollection && storedCollection . _getCollection )
43+ ? storedCollection . _getCollection ( ) . filesCollection
44+ : Meteor . connection . _mongo_livedata_collections [ this . data . atts . collection ] ;
45+ }
2846
29- this . collection = mongoCollection
30- ? mongoCollection . filesCollection
31- : global [ this . data . atts . collection ]
47+ if ( ! this . collection ) {
48+ throw new Meteor . Error ( 404 , `[meteor-autoform-files] No collection found by name "${ this . data . atts . collection } "` ,
49+ `Collection's name is case-sensetive. Please, make sure you're using right collection name.` ) ;
50+ }
3251
3352 this . uploadTemplate = this . data . atts . uploadTemplate || null ;
3453 this . previewTemplate = this . data . atts . previewTemplate || null ;
@@ -43,10 +62,6 @@ Template.afFileUpload.onCreated(function () {
4362 this . insertConfig . chunkSize = parseInt ( this . insertConfig . chunkSize ) ;
4463 }
4564
46- if ( ! this . collection ) {
47- throw new Meteor . Error ( 404 , '[meteor-autoform-files] No such collection "' + this . data . atts . collection + '"' ) ;
48- }
49-
5065 this . collectionName = function ( ) {
5166 return self . data . atts . collection ;
5267 } ;
0 commit comments