MChan
MChan

Reputation: 7212

Meteor dynamic schema using aldeed Simple Schema package

In my Meteor app, I save schemas in a collection called ClassifiedsTemp, now I am trying load those schemas retrieved from DB to generate a form for them using auto form and simple schema packages. So I am using the code shown below, but I am always getting the following error:

Uncaught TypeError: existingKey.indexOf is not a function

any thoughts what I might be doing wrong/missing here? Thanks

Client js code:

            this.classTempArrayToJSON = function(classTempArray) {
                var tempSchemaObj = {};
                for(var i=0; i<classTempArray.length; i++){
                    if(classTempArray[i].fieldtype=='String'){
                        tempSchemaObj[classTempArray[i].fieldname] = {  type: classTempArray[i].fieldtype,
                                                                       label: classTempArray[i].fieldlbl,
                                                                       min: Number(classTempArray[i].minval),
                                                                       max: Number(classTempArray[i].maxval),
                                                                       optional: !classTempArray[i].required };

                    }
                }
                return tempSchemaObj;
            };


    Template.SchemaGenTemp.events({
       'click #createSchema': function(e, t){

          var x = ClassifiedsTemp.find({}).fetch(); 
          var schema = JSON.stringify(classTempArrayToJSON(x[1].fieldsList));

          console.log(schema);

          SampleClassColSchema = new SimpleSchema(schema); //Crash here...

          console.log('Done');    
       }
    });

Output from JSON.stringify example:

{"Test":{"type":"String","label":"Car Price","min":1,"max":1000000,"optional":true}}

Upvotes: 3

Views: 1524

Answers (2)

SylvainB
SylvainB

Reputation: 4820

Two concerns:

  1. As Aleksei pointed out, you should not stringify your fieldsList.
  2. String is an actual type, it should not be between quotes.

Knowing this, try this code out:

        this.classTempArrayToJSON = function(classTempArray) {
            var tempSchemaObj = {};
            for(var i=0; i<classTempArray.length; i++){
                if(classTempArray[i].fieldtype=='String'){
                    tempSchemaObj[classTempArray[i].fieldname] = {  type: String, // since it will always be 'String'
                                                                   label: classTempArray[i].fieldlbl,
                                                                   min: Number(classTempArray[i].minval),
                                                                   max: Number(classTempArray[i].maxval),
                                                                   optional: !classTempArray[i].required };

                }
            }
            return tempSchemaObj;
        };


Template.SchemaGenTemp.events({
   'click #createSchema': function(e, t){

      var x = ClassifiedsTemp.find({}).fetch(); 
      var schema = classTempArrayToJSON(x[1].fieldsList);

      console.log(schema);

      SampleClassColSchema = new SimpleSchema(schema); // No crash here...

      console.log('Done');    
   }
});

Upvotes: 2

Kifir
Kifir

Reputation: 142

Actually you have to pass an object as parameter to SimpleSchema constructor.

In this case you don't need to use JSON.stringify on fetched object. Also you can use underscore _.each() method instead of for() loops.

Upvotes: 1

Related Questions