Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maximum call stack size exceeded when related models have different idAttribute #567

Open
zuzusik opened this issue Feb 3, 2020 · 0 comments

Comments

@zuzusik
Copy link
Contributor

zuzusik commented Feb 3, 2020

Description

JSData gets into infinite loop when trying to add item together with its relations to the store when idAttribute fields of related models are different.

Environment

  • js-data version: 3.0.8
  • node or browser version: any
  • operating system: any

Steps to reproduce

simple code to reproduce
const jsData = require('js-data');

const store = new jsData.DataStore();

store.defineMapper('user', {
  idAttribute: 'userId',
  relations: {
    hasOne: {
      profile: {
        foreignKey: 'user_id',
        localField: 'profile'
      }
    }
  }
});

store.defineMapper('profile', {
  idAttribute: 'profileId',
  relations: {
    belongsTo: {
      user: {
        foreignKey: 'user_id',
        localField: 'user'
      }
    }
  }
});

store.add('user', {
  userId: 1,
  profile: {
    profileId: 1,
    user_uid: 1
  }
})

Same code on RunKit: https://runkit.com/embed/frx2z0xbtijo

Possible Fix

As far as I see, there is an error on following line:

const relatedId = utils.get(relatedRecord, this.mapper.idAttribute)

Here this.mapper is not a mapper of relatedRecord, so taking it's idAttribute is not right.

Most likely, this line should be replaced with

const relatedId = this.relatedCollection.recordId(relatedRecord)

I will follow-up with a fix PR together with appropriate tests in upcoming days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant