Dynamic Meteor Schema Using Aldeed Simple Schema Package
In my Meteor app I save schemas in a collection called AdsTemp, now I am trying to load these schemas retrieved from DB to create a form for them using auto form and simple schema packages. So I use the code shown below, but I always get the following error:
Uncaught TypeError: existingKey.indexOf is not a function
Any thoughts what I might be doing wrong / missing here? Thanks to
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}}
+3
source to share
2 answers
Two problems:
- As Alexei pointed out, you shouldn't be pulling your List fields.
-
String
is the actual type, it must not be between quotes.
Knowing this, try this 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: 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');
}
});
+2
source to share