## plugin development – wp.media gallery collection sometimes undefined

Question

I have a function that includes an external image-source into the media-library (wp-admin/upload.php), by making an ajax-call to said image-source, formatting the response appropriately and inserting each image into the library with this function:

wp.media.frame.content.get('gallery').collection.add(formatted);


This works really well for me – most of the time.
For whatever reason, sometimes i get this exception in the debugger and the images are not added:

I tried looking at it in the debugger, and wp.media.frame.content.get('gallery') is fine, just collection is undefined.
In my PHP, on wp_enqueue_script I have ['media', 'media-grid', 'media-editor', 'media-views', 'jquery'] as dependencies, am I maybe missing one here?