Page 1 of 1

using object type with database

Posted: Sun Apr 20, 2014 1:13 am
by George6656971

Where is there an example of actually using a database object type in your code? Your tutorials and api docs are often worthless, because you never link to any real code being used.
Please help.
Thank you,
George

var selectedChildHistoryJSON = localStorage.getItem('_clipHistory');
var selectedClip = $(this).val();
var currentDate = new Date();
var selectedChildHistoryObj = JSON.parse(selectedChildHistoryJSON);
if (selectedChildHistoryObj === null) {
selectedChildHistoryObj = [];
}

Code: Select all

             var arrayLength = selectedChildHistoryObj.length; 
             var dateString = currentDate.toUTCString(); 

             var currentDateAndClip = { 
                 clipDate: dateString, 
                 clipValue: selectedClip 
             } 

             selectedChildHistoryObj[length] = currentDateAndClip; 
             selectedChildHistoryJSON = JSON.stringify(selectedChildHistoryObj); 

             localStorage.setItem('_clipHistory', selectedChildHistoryJSON);

using object type with database

Posted: Sun Apr 20, 2014 4:03 am
by Igor

Hello,

Below is an example of simple service request, everything else should be coded by your own.
http://docs.appery.io/documentation/b...

Do you want to save your data to the DB collection ? If so please see this topic:
https://getsatisfaction.com/apperyio/...


using object type with database

Posted: Sun Apr 20, 2014 6:09 am
by George6656971

Yes. I read this example. There is no clear instance of using code with an Object database type. With the appery database "object" type I've tried the below code and it seems stringify does not work. How do I save data to an Object data type? In such a way that parse, stringify work?

After the below code executes. I invoke a service that tries to save _clipHistory to the DB as an object.

The request payload looks like:
ClipHistory: "{"history":[{"clipDate":"Sun, 20 Apr 2014 02:09:54 GMT","clipValue":"5"}]}"
CurrentClip: "5"

and I get the following error:
"Invalid value for type 'object': '{"history":[{"clipDate":"Sun, 20 Apr 2014 02:09:54 GMT","clipValue":"5"}]}'."

If I enter the below directly in the db it works fine:
{"history":[{"clipDate":"Sun, 20 Apr 2014 02:09:54 GMT","clipValue":"5"}]}

Why am I getting an "Invalid value for type 'object':" error for something that I can manually type into the db?

Code: Select all

            var selectedChildHistoryJSON = localStorage.getItem('_clipHistory'); 
             var selectedClip = $(this).val(); 
             var currentDate = new Date(); 
             var selectedChildHistoryObj = JSON.parse(selectedChildHistoryJSON); 
             if (selectedChildHistoryObj === null) { 
                 selectedChildHistoryObj = { 
                     history: [] 
                 }; 
             } 

             var arrayLength = selectedChildHistoryObj.length; 
             var dateString = currentDate.toUTCString(); 

             var currentDateAndClip = { 
                 clipDate: dateString, 
                 clipValue: selectedClip 
             } 

             selectedChildHistoryObj.history[length] = currentDateAndClip; 
             selectedChildHistoryJSON = JSON.stringify(selectedChildHistoryObj); 

             localStorage.setItem('_clipHistory', selectedChildHistoryJSON);

using object type with database

Posted: Mon Apr 21, 2014 11:45 pm
by Illya Stepanov

Hi George,

We have replied you by email, and I post it here for everyone.

Please follow this steps below to understand how to work with object data type in DB:
ol
liCreate "object" collumn in your colletion. http://prntscr.com/3c78u0/direct/li

li Import update/create service for goal colleciton./li

li Navigate to this service and look at your object-type field. It should have "Object" in the value type select. http://prntscr.com/3c79vv/direct/li

li Open goal page-data tab and create datasource for this service and click "Edit mapping"./li

liNavigate to the request parameters - your variable name. (in our example called "someData"). And click "Edit JS"/"Add JS". http://prntscr.com/3c7ayl/direct/li

li JS editor appears. Populate it with following code:
precode
var objectToReturn = {};

objectToReturn.firstName = "Joe&quot
objectToReturn.lastName = "Black&quot
objectToReturn.phone = "2345667890&quot

objectToReturn.old = 43;

return objectToReturn;/code/pre
/li
/ol
Note: in this JS code we just used some constant values. But there is no limitations to use any dynamic data. For example: data from the components or data from the LSV.

After you invoke this service you will see how your row field is updated with value: http://prntscr.com/3c7c95/direct
pre
code
{"firstName":"Joe","lastName":"Black","phone":"2345667890","old":43}/code/pre

That's all.

Regards.


using object type with database

Posted: Tue Apr 22, 2014 5:11 am
by George6656971

From what you have posted it appears that what you are stating is that there is no need to translate to JSON in my code above. Objects are automatically stringified by your codes somewhere. Is that correct?


using object type with database

Posted: Tue Apr 22, 2014 5:34 am
by George6656971

Oh! Man. I went and read some stuff about local storage. Why didn't you just tell me that local storage does not work for arrays and I have to write some code like the below to store things there.

function saveGameState() {
if (!supportsLocalStorage()) { return false; }
localStorage["halma.game.in.progress"] = gGameInProgress;
for (var i = 0; i < kNumPieces; i++) {
localStorage["halma.piece." + i + ".row"] = gPieces.row;
localStorage["halma.piece." + i + ".column"] = gPieces.column;
}
localStorage["halma.selectedpiece"] = gSelectedPieceIndex;
localStorage["halma.selectedpiecehasmoved"] = gSelectedPieceHasMoved;
localStorage["halma.movecount"] = gMoveCount;
return true;
}


using object type with database

Posted: Tue Apr 22, 2014 1:31 pm
by Maryna Brodina

Hello!

Sorry for misunderstanding and thank you for sharing the code.
Do you need any further help?