Search code examples
javascriptmeteorimportpublish-subscribeundefined-behavior

Cannot read property 'insert' of undefined. Am I not publishing correctly?


My app was working perfectly till I externalized the collection definition from the client/main.js file to the ../imports/api/tasks.js file.

After this move I keep getting this error message in my browser: Uncaught TypeError: Cannot read property 'insert' of undefinedin my browser console. This error message points to line main.js:1206 which is:

/myApp
../client/main.js

import { Images } from '../imports/api/tasks.js';
Meteor.subscribe('Images');

FS.Utility.eachFile(event, function(file) {
var teste = Images.insert(file,  function (err, fileObj) {

var insertedIdSession = teste._id;
session.set("insertedBuyId", insertedIdSession);
Images.find().count());

  });

/myApp
../imports/api/tasks.js

import { Mongo } from "meteor/mongo";
Images = new FS.Collection("images", {
stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})] });

/myApp
../server/main.js

import { Images } from '../imports/api/tasks.js';

Meteor.publish('Images', function(){
return Images.find();
});

I have researched but failed to find a solution for this. Kind point out where i am going wrong.


Solution

  • It is telling you that "Images" is not defined.

    From what I can see of your code, ../imports/api/tasks.js does not export anything, which means that

    import { Images } from '../imports/api/tasks.js';
    

    won't give you anything, in fact it will replace the global variable Images with a null. So I think the solution is to replace the imports with this:

    import '../imports/api/tasks.js';
    

    Or you can put tasks.js in /common and that will do the same job