Stringfy simple schema object - Closed

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?

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.

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

1 Like

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

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

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.

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

1 Like

Kindly show what you mean with code.

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);


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

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?

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