BlairAllegroTech / js-data-jsonapi

JsonApi Adapter for js-data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hasOne relation localKey shouldn't be an Array

rgant opened this issue · comments

This is an odd issue, so please ask me for any clarifying information you think might help me document this issue better for you. Using findAll to get a list of 'reports' along with included accounts I am occasionally seeing this error but not for every Report in the list:

Error: "id" must be a string or a number!

This error appears to be from js-data.

These are the resources I've defined:

DS.defineResource({
    name: 'accounts'
});
DS.defineResource({
    name: 'reports',
    relations: {
        hasMany: {
            allocations: {
                localField: 'allocations',
                foreignKey: 'reportsid'
            }
        },
        hasOne: {
            accounts: {
                localField: 'grantee',
                localKey: 'accountid'
            }
        }
    }
});
DS.defineResource({
    name: 'allocations',
    relations: {
        hasOne: {
            reports: {
                localField: 'report',
                localKey: 'reportsid'
            }
        }
    }
});

And the query is:

DS.findAll('reports', {
    'include': 'allocations,grantee'
}, {
    headers: {
        // @TODO skip when will ready general authorization
        Authorization: 'Basic ' + LoginService.getToken()
    }
})
.then(function (rpts) {
    vm.items = rpts;
});

Some of the Reports come through fine:

{
  "stage": "Initial",
  "name": "Report 13",
  "status": "Feedback Provided",
  "created_at": "2016-02-03T10:30:58+00:00",
  "updated_at": "2016-06-11T17:40:17+00:00",
  "id": "1192",
  "accountid": "1617",
  "$_JSONAPIMETA_": {
    "selfType": "reports",
    "selfLink": "/v3/reports/1192",
    "isJsonApiReference": false,
    "relationships": {
      "allocations": {
        "related": {
          "type": "allocations",
          "url": "/v3/reports/1192/allocations"
        }
      },
      "grantee": {
        "related": {
          "type": "accounts",
          "url": "/v3/reports/1192/grantee"
        }
      }
    },
    "links": {
      "self": {
        "type": "self",
        "url": "/v3/reports/1192",
        "meta": {}
      }
    },
    "referenceCount": 1
  },
  "$$hashKey": "object:71"
}

But the very next one in the list has an array for the accountid field:

{
  "stage": "Final",
  "name": "Report 14",
  "status": "Feedback Provided",
  "created_at": "2016-02-03T10:35:59+00:00",
  "updated_at": "2016-06-11T17:40:34+00:00",
  "id": "1193",
  "accountid": [
    "1624"
  ],
  "$_JSONAPIMETA_": {
    "selfType": "reports",
    "selfLink": "/v3/reports/1193",
    "isJsonApiReference": false,
    "relationships": {
      "allocations": {
        "related": {
          "type": "allocations",
          "url": "/v3/reports/1193/allocations"
        }
      },
      "grantee": {
        "related": {
          "type": "accounts",
          "url": "/v3/reports/1193/grantee"
        }
      }
    },
    "links": {
      "self": {
        "type": "self",
        "url": "/v3/reports/1193",
        "meta": {}
      }
    },
    "referenceCount": 1
  },
  "$$hashKey": "object:72"
}

@rgant Can you post your configuration for 'accounts' pls

All I've done for accounts is define the name:

DS.defineResource({name: 'accounts'});

Do you think that is a problem?

On Fri, Jul 8, 2016, 00:50 Blair notifications@github.com wrote:

@rgant https://github.com/rgant Can you post your configuration for
'accounts' pls


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#18 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AArXmQB_uKv64nyl3Ps4laUfh5lYUZeVks5qTdcPgaJpZM4JHscl
.

That certainly looks like it worked on my end! Thanks again. This is making my angular app so much neater.