Meteor-CollectionFS
Meteor-CollectionFS copied to clipboard
fileObj size not set
After removing meteor insecure an creating my method, i get this error '/cfs.ProductsImages.filerecord/insert': Error: TransferQueue upload failed: fileObj size not set
Any help would be fantastic.
My insert code:
Meteor.methods({
addProduct: function(file, name, category, description, is_featured) {
if(file) {
var fsFile = new FS.File(file);
ProductsImages.insert(fsFile, function(err, result) {
if(!err) {
var productImage = "/cfs/files/ProductsImages/" + result._id;
Products.insert({
name: name,
category: category,
description: description,
is_featured: is_featured,
image: productImage,
createdAt: new Date()
})
FlashMessages.sendSuccess("Product Added");
} else {
FlashMessages.sendError("A problem has occured with uploading of image.");
}
});
} else {
if(!err) {
var productImage = "/images/noimage.png";
Products.insert({
name: name,
category: category,
description: description,
is_featured: is_featured,
image: productImage,
createdAt: new Date()
})
FlashMessages.sendSuccess("Product Added");
} else {
FlashMessages.sendError("A problem has occured with uploading of image.");
}
};
}
});
Is this a common, server or client method? It should be on the client. Do you set the allow rules for those collections? Where and what is the "file" you pipe in?
You should (imo) save the image _id (instead of a selfmade hardcoded path) and use a (global) helper to get the image url directly out of the image collection or otherwise return a 404 image path.
Template.registerHelper('imageUrlWithId', function(id){
var image = Images.findOne(id)
if (image && image.url()) {
return image.url()
} else {
return '/noimage.jpg'
}
}
<template name="foo">
{{#each products}}
<img src={{imageUrlWithId image}}>
{{/each}}
</template>
Oh and also keep in mind that the Images.insert callback returns the _id instantly, it's not uploaded yet. So if you remove the upload element on return of your method, it might break the upload. just my 2cent
Thanks for the quick reply. The method is on the client side and I have set the allow rules like this:
ProductsImages = new FS.Collection("ProductsImages", {
stores: [new FS.Store.GridFS("ProductsImages")]
});
ProductsImages.allow({
insert: function(fileId, doc) {
return true;
},
download: function(fileId, doc) {
return true;
}
})
And this is the event the file is coming from:
Template.add_product.events({
"submit, .add_product": function(event) {
var name = event.target.name.value;
var category = event.target.category.value;
var description = event.target.description.value;
var is_featured = event.target.is_featured.value;
var file = $("#productImage").get(0).files[0];
Meteor.call("addProduct", file, name, category, description, is_featured);
event.target.name.value = "";
event.target.category.value = "";
event.target.description.value = "";
event.target.is_featured.value = "";
Router.go("/");
return false;
}
});
What happens when you comment out the Router.go("/") line?
Because the upload is most likely not finished at that point.
You can also add
FS.debug = true
in common code to see more debug info.
So, how to fix this. I meet this case too. Help me,plz
I have the same issue was there ever a solution?