Page 1 of 2

Query with Where condition invoked by Javascript

Posted: Wed Mar 20, 2013 11:40 pm
by Janko

Hi,

I am struggling to invoke a DB REST service from Javascript with a where condition.
I always get all the records. This is the (last) relevant part of the code I am using:

recordId = 123456 // it's inside a loop, changing each loop
QueryToLaunch='{where={"FieldName": {"collName":"LinkedCollectionName", "_id":"' + recordId + '"}}}'
ServiceDefined.execute(QueryToLaunch)

ServiceDefined is a query service on aTiggzi DB collection. I tried both having and not having the "where" (blank) parameters defined in the service itself (what is the right solution by the way?) with no luck.

I already look at ANY documentation I found, tried several solution, but none worked.
Please provide the right javascript code, don't point me to uncomplete documentation.

Thanks! Marco.


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 12:13 am
by maxkatz

Please check in Chrome Dev. Tools Network (or Firebug) the request that's being sent, to make sure it has the right syntax. If still not working, please paste the entire request that's being sent.


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 12:22 am
by Janko

Thanx .. Uhm.. There must be something definitely not working as expected !!! ....

With this QueryToLaunch var
where={'BikeArea': {'collName':'BikeArea', '_id':'5138db5ee4b0fa3619e9d683'}}

the following request /response is produced.
note that each char in the QueryToLaunch generated a separate URL parameters !!!! (note aso that I deleted for security reasons both project ID from URL and DB ID):

Request URL:https://api.tiggzi.com/rest/1/db/coll...
Request Method:GET
Status Code:200 OK

Request Headersview source
Accept:application/json, text/javascript, /; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4,en-GB;q=0.2,fr;q=0.2
Connection:keep-alive
Host:api.tiggzi.com
Origin:http://project.tiggzi.com
Referer:http://project.tiggzi.com/view/xxxxxx...
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
X-Tiggzi-Database-Id:xxxxxxxxxxxxxxxxxxxxx

Query String Parametersview parsed
0=w&1=h&2=e&3=r&4=e&5=%3D&6=%25&7=7&8=B&9='&10=B&11=i&12=k&13=e&14=A&15=r&16=e&17=a&18='&19=%3A&20=%25&21=2&22=0&23=%25&24=7&25=B&26='&27=c&28=o&29=l&30=l&31=N&32=a&33=m&34=e&35='&36=%3A&37='&38=B&39=i&40=k&41=e&42=A&43=r&44=e&45=a&46='&47=%2C&48=%25&49=2&50=0&51='&52=_&53=i&54=d&55='&56=%3A&57='&58=5&59=1&60=3&61=8&62=d&63=b&64=5&65=e&66=e&67=4&68=b&69=0&70=f&71=a&72=3&73=6&74=1&75=9&76=e&77=9&78=d&79=6&80=8&81=3&82='&83=%25&84=7&85=D&86=%25&87=7&88=D

Response Headersview source
Access-Control-Allow-Origin:http://project.tiggzi.com
Connection:keep-alive
Content-Type:application/json
Date:Thu, 21 Mar 2013 00:14:37 GMT
Server:nginx
Set-Cookie:JSESSIONID=SFqM1zfjzgkgjkjgbkj.undefined; Path=/; Secure
Transfer-Encoding:chunked


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 12:31 am
by maxkatz

As you are sending a request from custom JavaScript (not from an app built in Tiggzi), I don't really know why this happens. The "where" parameter should be urlencoded.


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 12:53 am
by Janko

Ok.

Isn't this
ServiceDefined.execute(QueryToLaunch)

native Tiggzi code?
(I got it from http://help.gotiggr.com/documentation...)

If not, how can i invoke a service via Javascript passing to it the query condition ?


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 12:55 am
by maxkatz

Are you invoking the service from a Tiggzi-built app or an app built completely outside of Tiggzi?


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 3:27 pm
by Janko

Hi,
Tiggzi completely built-in app.


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 4:37 pm
by maxkatz

To pass data to the service this way would look like this:

code
service_instance.execute({'data': {'where': '....'}});
/code


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 6:41 pm
by Janko

Thanks Max.. we are on the right way but not still there I believe.

it seems like any string included in {} is slit in char and each char treated as a parameters in the querystring.

this Instancename.execute({ "data" : "'where': '{'_id':'5140eb66e4b0fa3619eaade8'}' " })
causes this:

Query String Parametersview sourceview URL encoded
0:'
1:w
2:h
3:e
4:r
5:e
6:'
7::
8:
9:'
10:{
11:'
12:_
13:i
14:d
15:'
16::
17:'
18:5
19:1
20:4
21:0
22:e
23:b
24:6
25:6
26:e
27:4
28:b
29:0
30:f
31:a
32:3
33:6
34:1
35:9
36:e
37:a
38:a
39:d
40:e
41:8
42:'
43:}
44:'
45:

If i try as you suggested, like:
.execute({'data' : {'where':{'_id': '5140eb66e4b0fa3619eaade8'}} })
this is how the URl is encoded:
https://api.tiggzi.com/rest/1/db/coll...

How should I correct this?
thx


Query with Where condition invoked by Javascript

Posted: Thu Mar 21, 2013 10:09 pm
by Maryna Brodina

Hello! Try this
codeInstancename.execute({ "data" : {"where": '{"_id":"5140eb66e4b0fa3619eaade8"}'}});/code

Single and double quotes are important