jasonkneen / RESTe

A simple JavaScript REST / API helper for Titanium

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handling response from ArrowDB

adamtarmstrong opened this issue · comments

When fetching "Posts" from ArrowDB, Appcelerator is returning the posts as an array ("posts") but inside a property ("response") and not in the root.

{
   "meta": {
         "code":200,
         "status":"ok",
         "method_name":"queryPosts",
         "count":1},
   "response":{
         "posts":[{
               "id":"597cd03010859c094b238be2",
               "title":"Test1",
               "created_at":"2017-07-29T18:13:04+0000",
               "user_id":"597cc93f119417a96570608d"
         }]
   }
}

I tried configuring my Collection both ways:

collections: [{
      name: "queryPosts",
      content: "response[posts]",
      read: "getQueryPosts"
},....

and I tried

collections: [{
      name: "queryPosts",
      content: "response.posts",
      read: "getQueryPosts"
},....

And neither one seem to work. Am I doing something wrong to attach the "content" to the array of posts?

Thanks,
Adam.

I tried adding a transform to the model like referenced in some of your other (closed) tickets but can't seem to get it to work either. I thought the transform should pull out the 'response' object and then the collection would be able to see 'posts' array in the root. Is that not correct?

models: [{
        name: "name",
        id: "id",
        //content: "response",
        transform: function(m) {
            var transform = m.toJSON();
            transform = m.response;
            return transform;
        },
        collections: [{
            name: "queryPosts",
            content: "posts",
            read: "getQueryPosts"
        }],
    }],
    methods: [{
        name: "getQueryPosts",
        get: "posts/query.json?key=KEY_VALUE_HERE&pretty_json=true&count=true",
        ...

I think this is the same as #46. If we could set the parent, I think you would get what you want.

@raybelisle You are absolutely correct. I just updated your Issue with my work around. And since this is a dup of yours I'll close mine.

Gonna check your workaround :)