Search code examples
ecmascript-6parse-platformparse-server

Destruct Parse.Object instance


I would like to know an easy way to destruct a Parse.Object instance.

Lets say I have an Parse.Object instance with following attributes:

const Address = new Parse.Object<Address>("Address", {
   address: "St. Nowhere",
   zipCode: 33111,
   timezoneOffset: -2,
   dayLightSavingTime: true
})

I want to destruct Address to get is't properties values easily. Like:

const {
   address,
   zipCode,
   ...OtherAttributes
} = Address

Solution

  • Well I found that it is easy as:

    const {
      address,
      zipCode,
      ...OtherAttributes
    } = Address.toJSON()
    

    Be advised that if you are using Typescript and Address is possible undefined you will get the error:

    TS2339: Property 'address' does not exist on type '(ToJSON  & JSONBaseAttributes) | undefined'.
    

    One solution would be:

    const {
      address,
      zipCode,
      ...OtherAttributes
    } = Address.toJSON() ?? {}
    

    Caution with Date attributes. They are transformed to { __type: 'Date'; iso: string; } where iso is the date in ISO 8601 format.