11
votes

I've just started learning on MEAN stack and need to generate dynamic forms on the fly.

The requirement is to import document (excel/csv/xml/xls etc..) and generate dynamic forms using it so a user can update their data and again export it into the respective file.

So to accomplish this I'm converting documents to JSON format and storing JSON data into the MongoDB database.

Ex: Consider this xlsx data:

ID  Name       dob        Gender
1   user1      7-Dec-87   m
2   user2      8-Dec-87   f
3   user3      9-Dec-87   f
3   user4      4-Dec-87   m

And I'm converting this using xlsx-to-json module to JSON format and storing it into Mongodb.

app.post('/myapp', function (req, res) {

    //console.log("===========" + req.file.path);

    converter({
        input: req.file.path,
        output: "output.json"
    }, function (err, result) {
        if (err) {
            console.error(err);
        } else {
            console.log(result);
            db.collection('test').insert(result, function (err, doc) {
                console.log(err);
                res.json(doc);
            });
        }
    });

});

Here I'm fetching above data from Mongodb & express.js

app.get('/myapp', function (req, res) {
    db.collection('test').find(function (err, docs) {
        console.log(docs);
        res.json(docs);
    });
});

app.get('/birthdaylist/:id', function (req, res) {
    var id = req.params.id;
    console.log(id);
    db.collection('test').findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
        console.log(JSON.stringify(doc));
        res.json(doc);
    });
});

and here's the JSON output:

[ 
  { dob: '7-Dec-87', ID: '1', Name: 'user1' },
  { dob: '8-Dec-87', ID: '2', Name: 'user2' },
  { dob: '9-Dec-87', ID: '3', Name: 'user3' },
  { dob: '4-Dec-87', ID: '4', Name: 'user4' } 
]

So, I've few queries:

  • Is this the correct approach I'm doing to generate dynamic form from xlsx/csv..etc ? If yes, then how can I generate dynamic form from above JSON.

  • While exploring on google I've found mongodb generates form automatically (https://github.com/GothAck/forms-mongoose) So will it help because there may be chance of huge data on excel files.

Any help would be really appreciated.

2
The above code looks fine for conversion, especially considering your output looks good. Without an attempt at the front end code, SO can't help you much. The github link you include is for Mongoose so unless you're using mongoose, it's not going to help you much.metame
And just as a small note on MongoDB, you're not actually storing JSON in Mongo, but BSON.metame
@metame: Thanks for replying. I tried moogose but its requires proper attributes in JSON format. In my case JSON is generating from excel file so how can I tackle that issue?J.K.A.
@metame: Ohh so its BSON.. Actually I'm using mongojs nodes module for inserting dataJ.K.A.
That sounds like another question that is a lot more clearer. I would recommend posting that as a separate question or editing your question (and title) to ask that instead. The way your current question is stated, a proper answer would require someone to write a bunch of code for you.metame

2 Answers

4
votes

Do you actually need to analyze an arbitrary spreadsheet and dynamically extract the schema, or do you know the schema ahead of time? If you know the schema, then the Mongoose form generating example is straightforward. But make sure that is actually a requirement because it is tough.

You are never going to be 100% because spreadsheets are created by users and users do weird things. But you can make something that works most of the time.

You need something that takes a JSON object and extracts the schema and puts that in a Mongoose schema format.

So you want to add an interesting module to Mongoose schema. I searched node-modules.com and this came up: https://github.com/Nijikokun/generate-schema

4
votes

Form generation is not a trivial task. You may want to consider using a library for this. Here are a few that might be useful to you:

http://schemaform.io/

https://github.com/jdorn/json-editor/

Also, if you need help generating JSON schema from JSON:

http://jsonschema.net/#/

and of course: http://json-schema.org/