简体   繁体   中英

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 undefined in 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.

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

You need to export your Images collection in order to import it to other files, like this:

import { Mongo } from "meteor/mongo";

export const Images = new FS.Collection("images", {
  stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM