Stringfy simple schema object - Closed


#1

I have this simple schema object

Schema.Rides = new SimpleSchema({
  brand: {
    type: String,
    label: "搜尋",
    max: 100
  },
  model: {
    type: String,
    label: "Tìm kiếm",
    max: 100
  },
  fueltype: {
    type: String,
    label: "Пошук",
    allowedValues: ['Petrol', 'Diesel', 'Hybrid', 'Electric'],
  },
  bodystyle: {
    type: String,
    label: "بحث",
    allowedValues: ['Convertibles', 'Coupes', 'Hatchbacks', 'Vans', 'Sedans', 'Suvs', 'Trucks', 'Wagons'],
    optional: true
  },
  topspeed: {
    type: Number,
    label: "חיפוש",
    optional: true
  },
  power: {
    type: Number,
    label: "Power (HP)",
    optional: true
  }
});

If i stringfy console.log(JSON.stringify(Schema.Rides));

and try and get the object back

console.log(JSON.parse(JSON.stringify(Schema.Rides)));

i do not end up with the original object. Why is this?


#2

Just a guess: sometimes JS objects have “loops” in their inner structure…
So they have references to sub-objects that and these sub objects themself reference stuff that is somewhere else upwards in the object tree.


#3

stringify does not take the constructor into account. It loses all its functions.


#4

I tried reviving an autoform schema and i just could not make it work. I am exploring other methods.


#5

Just stringify the object literal argument you’re passing to the constructor. Do you really need to stringify the whole object instance?


#6

Yes,i need to stringfy the whole thing since i want to store it in a mongodb then retrieve it to create the forms.

I want now to use simple literal object like

{[{“firstnames”: “edward”,“type”:“text”,“required”:“true”,“length”:“56”},{“lastnames”: “null”,“type”:“text”,“required”:“false”,“default”:“jim duggan”,“length”:“54”}]}

and ditch autoforms alltogether.


#7

Couldn’t you just store the the object literal in mongo and then, after querying the database, use it to create a SimpleSchema instance?


#8

Kindly show what you mean with code.


#9

Something like:

const schemaObj = {
  brand: {
    type: String,
    label: "搜尋",
    max: 100
  },
  model: {
    type: String,
    label: "Tìm kiếm",
    max: 100
  },
  fueltype: {
    type: String,
    label: "Пошук",
    allowedValues: ['Petrol', 'Diesel', 'Hybrid', 'Electric'],
  },
  bodystyle: {
    type: String,
    label: "بحث",
    allowedValues: ['Convertibles', 'Coupes', 'Hatchbacks', 'Vans', 'Sedans', 'Suvs', 'Trucks', 'Wagons'],
    optional: true
  },
  topspeed: {
    type: Number,
    label: "חיפוש",
    optional: true
  },
  power: {
    type: Number,
    label: "Power (HP)",
    optional: true
  }
};
Schema.Rides = new SimpleSchema(schemaObj);

JSON.stringify(schemaObj);



#10

That may be soo, but down the line you will need to revive json and thats a whole lot difficult.


#11

Well, wouldn’t you just JSON.parse the field retrieved from the db to get the object back against? Isn’t that what you were trying to do?


#12

I am no longer interested in pursuing a solution for this problem using autoform. Thanks all for your replies.