Closed sohailalam2 closed 6 years ago
Hi @sohailalam2
It is acually the intended behaviour. The default properties are set on the object prototype, not the object itself. If you serialize an object to JSON then parse the JSON and pass it to the model constructor, the defaults will apply again and you will get the same object. This is needed because changing the defaults of a model should update default values on all previously created instances.
If you want the missing properties to be set at object instanciation as own properties with the default values, this is no longer "default properties", it is more like a "one-time assignment". So you can do this :
const FileInfo = ObjectModel({
name: String,
size: [Number],
creationDate: [Date],
writable: Boolean
})
FileInfo.create = function(props){
const defaultProps = {
name: "Untitled file",
size: 0,
writable: true
}
return new FileInfo( Object.assign(defaultProps, props) )
}
let file = FileInfo.create({ writable: false });
edit: I initially thought you were right about Object.keys, but after rereading the docs this method should only return the own properties of an object, not those inherited from the prototype. To get all the properties including those in the prototype, you can use a for..in
loop
Thanks @sylvainpolletvillard for your comments.
Actually I am not worried about default properties whatsoever. My requirement is as follows -
The step 3 is where I am facing an issue because doing a JSON.stringify does not give the entire schema. So I was wondering how can I get the entire schema with default values (if they are not defined) and then save the values into a database.
for..in
works but I wonder if this is the right way to do it!
Did you try the code I posted above ? It should do what you are looking for.
Closed due to inactivity
The ObjectModel when serialized only shows properties which were passed while creating the object. However, the created object does have the default values however they are not serialized.
How can I get all the defined properties from the model?
Compute Environment: OS: Windows 10 JS: NodeJS v9.3.0