0

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
            }
        });
    });
}
user3142695
  • 15,844
  • 47
  • 176
  • 332
  • Also see [this question](https://stackoverflow.com/questions/29690350/how-would-you-find-a-collection-dynamically-in-meteor-with-a-value) for a slightly more complex example. – David Weldon Sep 06 '15 at 04:04
  • I tried `window[collection].insert(JsonObj[i]);` for inserting data, as `collection` gets the value. But I get the error `Uncaught TypeError: Cannot read property 'insert' of undefined`. Also I don't know how to remove the data like I do it in server.js: Meteor.call('removeAllPosts'); Also need to use `collection`. – user3142695 Sep 06 '15 at 04:53
  • As shown in the examples, the name must match __exactly__. `window['cars']` isn't `window['Cars']`isn't `window[' Cars ']`. You should `console.log` the name of the string you are extracting from the input before you use it. The latter question about how to use a method seems orthogonal to the core question posed here. – David Weldon Sep 06 '15 at 05:45
  • If I do `console.log(window);` there isn't 'Cars' at all. But I did `Cars = new Meteor.Collection('Cars');` So I don't understand what is missing... – user3142695 Sep 06 '15 at 09:51
  • If you open the console in your browser, type `Cars`, and get `undefined`, then the collection isn't defined on the client. Make sure you add something like `Cars = new Mongo.Collection('cars');` (without the `var`) to a file in a file common to both the client and the server - e.g. `lib/collections/cars.js`. – David Weldon Sep 06 '15 at 14:43

0 Answers0