When I start the code, the registration of a single element works. If I run the code a second time, I have a console error when saving in JSON format.
My codeconsole wrote:Uncaught TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at spider_JSON_DumpStructure (spiderbasic.js:284)
at spider_JSON_DumpField (spiderbasic.js:283)
at spider_InsertJSONList (spiderbasic.js:284)
at f_savedata (spiderbasic.js:306)
Code: Select all
EnableExplicit
Declare Start()
Declare InsertData()
Declare ShowResult()
Declare LoadData()
Declare SaveData()
Structure NewRecord
name.s
phone.s
EndStructure
Global NewList Contacts.NewRecord()
Start()
Procedure Start()
LoadData()
InsertData()
ShowResult()
SaveData()
EndProcedure
Procedure InsertData()
Protected n = ListSize(Contacts())
AddElement(Contacts())
With Contacts()
\name = "Name " + Str(n)
\phone= LSet(Str(n), 6 , Str(n))
EndWith
EndProcedure
Procedure ShowResult()
ForEach Contacts()
With Contacts()
Debug \name + " - " + \phone
EndWith
Next
EndProcedure
Procedure LoadData()
Protected Buffer.s = ""
!v_buffer = localStorage.getItem('myStorage');
!if (v_buffer === null) { v_buffer = "" }
If Len(Buffer) <> 0
If ParseJSON(0, Buffer)
Debug "Load " + Buffer
If Buffer <> #Null$
ExtractJSONList(JSONValue(0), Contacts())
EndIf
EndIf
Else
Debug "no data in local storage"
EndIf
EndProcedure
Procedure SaveData()
Protected Buffer.s
If CreateJSON(0)
InsertJSONList(JSONValue(0), Contacts()) ;<- Console : Cannot convert undefined or null to object
Buffer = ComposeJSON(0)
!localStorage.setItem('myStorage', v_buffer);
EndIf
EndProcedure
Thank you in advance for your suggestions.