-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
BaseModel.toObject()
fails with null
nested BelongsTo relationship
#1008
Comments
Before you create a PR, can you please create a failing test in this repo? Lucid has many moving parts, so first we should be able to reproduce the issue in a test before we can accept any PRs for the code change |
Sure, I can try. Do you mean submit a PR with the test? I'll admit I've not done any tests with Adonis/Japa before, so might need to figure some stuff out first. |
Yes, it should be based on https://github.com/adonisjs/lucid/tree/v18 branch.
Feel free to ask for help if you feel stuck |
Okay, I've got this so far: // TODO: I'm not really checking the author, so can probably just remove...
test('add preloaded belongsTo relationship to toObject result', async ({ assert }) => {
class Category extends BaseModel {
@column()
public name: string
@column()
public parentId: number
@belongsTo(() => Category)
public parent: BelongsTo<typeof Category>
}
class Post extends BaseModel {
@column()
public title: string
@column()
public userId: number
@belongsTo(() => User)
public author: BelongsTo<typeof User>
@hasOne(() => Category)
public category: HasOne<typeof Category>
}
class User extends BaseModel {
@column({ isPrimary: true })
public id: number
@column()
public username: string
@hasMany(() => Post)
public posts: HasMany<typeof Post>
}
const user = new User()
user.username = 'virk'
const category = new Category()
category.name = 'Tutorials'
const subCategory = new Category()
subCategory.name = 'Lucid'
const post = new Post()
post.title = 'Adonis 101'
// user.$setRelated('posts', [post])
post.$setRelated('author', user)
subCategory.$setRelated('parent', category)
post.$setRelated('category', subCategory)
// passes
assert.deepEqual(subCategory.toObject(), {
name: 'Lucid',
parent: {
name: 'Tutorials',
$extras: {},
},
$extras: {},
})
// fails
assert.deepEqual(category.toObject(), {
name: 'Tutorials',
parent: null,
$extras: {},
})
}).pin() I don't think I actually need the Post/Author stuff, so should I remove that? I'm also not sure about the parent being null, since we're not actually setting the I can just make a draft PR with what I have if that's better than here :) |
Package version
18.4.2
Describe the bug
I have a model that has a nested nullable BelongsTo relationship, and when I try to use
.toObject()
on it, it gives me a TypeError: Cannot read properties of null (reading 'toObject')The model is like this:
Part > Item > PartCategory
PartCategory has a
parent_id: number | null
column field, and aparent
relationship like so:When I query my Part model, it works if the category has a parent, but fails when the category has no parent.
I was able to fix this by overriding
BaseModel.toObject()
in myPartCategory
model, to check for falsey values before trying to call.toObject()
on them.Is what I'm doing correct? Is this a bug, or am I doing something wrong? I can submit a PR with this fix if I'm correct in my diagnosis.
Full stack trace:
Reproduction repo
No response
The text was updated successfully, but these errors were encountered: