Skip to content

lb-ng: Issue when working with related models #221

@markusiseli

Description

@markusiseli

Given two simple related models: user and answer, defined as follows:
user.json:

...
"relations": {
    "answers": {
    "type": "hasMany",
    "model": "answer",
}
...

and answer.json:

...
"relations": {
    "user": {
    "type": "belongsTo",
    "model": "user"
}
...

The client-side code runs fine, does all the user authentication etc. but creating an answer instance, given the user model using User.answers.create({"id": some_user_id}, {some_answer_json}); throws an Uncaught TypeError: Cannot set property 'create' of undefined.

I tracked the error back to lb-ng: lb-ng creates the following code (lb-services.js) for creation of an answer instance, given the user (R in this case is the user resource):

...
R.answers.create = function() {
    var TargetResource = $injector.get("Answer");
    var action = TargetResource["::create::user::answers"];
    return action.apply(R, arguments);
};
...

The issue with this code is that the property answers of R - as in R.answers - was not defined before defining R.answers.create, hence the error. This error occurs with all similar methods on related models, where the relation name is inserted: e.g. R.answers.createMany, etc.

Here is my very temporary fix:
In lb-services.js, manually initialize R.answers = {}; before the line R.anwers.create = function() { works and the answer instance gets created with the creatorId in the DB.

If this is a bug, I'd be glad to help fix it, but would need instructions. If this is not a bug, please let me know what is wrong with my understanding.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions