Page 1 of 1

Using JSON and jQuery in server scripts

Posted: Sat Apr 19, 2014 12:47 pm
by Tom Mccann

I'm trying to parse the result of a database query in a server script. The query works and the result comes back ok.

code
result.query = Collection.query(DB_id, "Product", params, token);
console.log(result);
/code

The result variable that is returned from the query brings back the object containing an array. It looks like this on the trace:

{ query: [ { _createdAt: [Object], _id: '5221c2a4e4b0899403d2afb5', _updatedAt: [Object], acl: [Object], available: true, category1: 'Pasta', description: 'The classic dish', entity_code: 'stef1', name: 'Spaghetti', price: 23, productlist_code: 'A', sort_order: 3 }, { _createdAt: [Object], _id: '535269b5e4b0e44dd0271627', _updatedAt: [Object], acl: [Object], available: true, category1: 'Pasta', description: 'Lovely blah', entity_code: 'stef1', name: 'Blah', price: 20, productlist_code: 'A', sort_order: 5 } ] }

That looks good to me. However, if I try to iterate the result to get the object ids using a loop like this:

code
for (var i = 0; i < result&#46;length; i++) { etc&#46;&#46;&#46;
/code

then it says that result.length is undefined. I guess this might be because result is a string and not an object?

If I try to turn result into an object using:

code
var obj = jQuery&#46;parseJSON( result );
/code

I get the following error message: jQuery is not definedncode: undefined

My questions: (1) why is jQuery not recognised?
(2) how do you recommend parsing the json that is returned from the query?


Using JSON and jQuery in server scripts

Posted: Sat Apr 19, 2014 2:11 pm
by Illya Stepanov

Hi Tom,

Are you using this code as a server code script?

The DB returns an object (for more info please check here: http://docs.appery.io/documentation/b...), so here you should parse the object first and after iterating loop sequence process the data.

Use codeJSON&#46;parse();/code


Using JSON and jQuery in server scripts

Posted: Sat Apr 19, 2014 8:45 pm
by Tom Mccann

Thank you. My follow up questions is why can't I use JSON.parse in a server script?

I'm using this line of code:

code
var obj = JSON&#46;parse( result );
/code

I'm getting an exception with this message:

Unexpected token oncode: undefined


Using JSON and jQuery in server scripts

Posted: Sun Apr 20, 2014 9:17 pm
by Illya Stepanov

Hi Tom,

We need more information - can you show us all parts of your code?


Using JSON and jQuery in server scripts

Posted: Thu Apr 24, 2014 3:22 pm
by Kateryna Grynko

Hi Tom,

Could you please show the code? Did you solve it?