This is how I'm reading a json-file to import it to a mongoDB in a meteor-app. But as you can see in the Template, I want to use a #collection-input field. So the user can select the collection to which the json-data belongs. ie. if collection = cars, the json-data should be saved to the Collection 'Cars' and if you type 'Flowers', all data will be inserted in 'Flowers'.
How do I get this dynamically (as I don't know how to use the value of #collection in the collections.js and server.js)?
Template
<template name="jsonImport">
<form id="importJson">
<input type="text" id="collection">
<input type="file" id="files" name="file" />
</form>
</template>
events.js
Template.jsonImport.events({
'change #files': function(evt) {
var file = evt.target.files[0],
reader = new FileReader(),
collection = document.getElementById('collection').value; // use this collection
JsonObj = null;
reader.onload = (function(theFile) {
return function(e) {
JsonObj = JSON.parse(e.target.result);
Meteor.call('removeAllPosts'); // Remove all data from the collection
for (var i = 0; i < JsonObj.length; i++) {
Cars.insert(JsonObj[i]); // should be var collection
}
};
})(file);
reader.readAsText(file);
}
});
collections.js
Cars = new Meteor.Collection('Cars'); // should be var collection
Cars.allow({ // should be var collection
insert: function(userId) {
return true;
}
});
server.js
if (Meteor.isServer) {
Meteor.startup(function() {
return Meteor.methods({
removeAllPosts: function() {
return Cars.remove({}); // should be var collection
}
});
});
}