I did the same the the schema document, but I got Error: Match error: Expected plain object.
What I want to do is to add a customised field to each of uploaded images.
Can you please advice how to solve it?
Warm regards
Hi @Beable2 ,
To help you I need:
@Beable2 any news on your end?
Hi Dimitru,
Thanks you very much for your prompt rely.
I want to change the the default upload image schema with the following:
var defaultSchema = {
size: {
type: Number
},
name: {
type: String
},
type: {
type: String
},
path: {
type: String
},
isVideo: {
type: Boolean
},
isAudio: {
type: Boolean
},
isImage: {
type: Boolean
},
isText: {
type: Boolean
},
isJSON: {
type: Boolean
},
isPDF: {
type: Boolean
},
extension: {
type: String,
optional: true
},
_storagePath: {
type: String
},
_downloadRoute: {
type: String
},
_collectionName: {
type: String
},
public: {
type: Boolean,
optional: true
},
meta: {
type: Object,
blackbox: true,
optional: true
},
userId: {
type: String,
optional: true
},
updatedAt: {
type: Date,
optional: true
},
versions: {
type: Object,
blackbox: true
}
};
var mySchema = _.extend(defaultSchema, {
productId: {
type: String
}
});
UploadImages = new Meteor.Files({
debug: true,
//storagePath: process.env.PWD + '/public/UploadedImages/', //get the root public folder
collectionName: 'UploadedImages',
allowClientCode: false, // Disallow remove files from Client
schema:mySchema,
onBeforeUpload: function (file) {
// Allow upload files under 10MB, and only in png/jpg/jpeg formats
if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
return true;
} else {
return 'Please upload image, with size equal or less than 10MB; png, jpeg, jpg ONLY';
}
}
});
On the template :
Template.UploadImages.events({
'change #fileInput': function(e, template) {
if (e.currentTarget.files && e.currentTarget.files[0]) {
// We upload only one file, in case
// there was multiple files selected
var file = e.currentTarget.files[0];
**file["productId"] = "This is product ID";**
if (file) {
var uploadInstance = UploadImages.insert({
file: file,
streams: 'dynamic',
chunkSize: 'dynamic'
}, false);
uploadInstance.on('start', function() {
template.currentUpload.set(this);
});
uploadInstance.on('end', function(error, fileObj) {
if (error) {
console.log('Error during upload: ' + error.reason);
} else {
console.log('File "' + fileObj.name + '" successfully uploaded');
}
template.currentUpload.set(false);
});
uploadInstance.start();
}
}
}
});
I want to update the records in the uploaded collections with product ID. I modify the file with new field like this file["productId"] = "This is product ID"; to amend the object to be insert into the collections.
I printed out the file
File {productId: "This is product ID", name: "icon.jpg", lastModified: 1468201370000, lastModifiedDate: Mon Jul 11 2016 11:42:50 GMT+1000 (AEST), webkitRelativePath: ""鈥
As you can see productId is there but not in the collection.
Thanks you very much for your time and help in advance.
Warm regards
I believe meta object is the right place for productId, just pass it into UploadImages.insert({meta: {productId: 'YourProdID'}, /* other options */}, false);.
Otherwise you need to use onAfterUpload hook to add extra property.
Thank you
Most helpful comment
Thank you