Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
TypeError Cannot read properties of undefined (reading 'related')

I am trying to follow a tutorial from someone who created a full website using Adonisjs, almost everything was exactly like showed in the tutorial, exept that the frontend was a little different, but the results were the same. Now I am trying to get the posts showed in the browser, but when I try I get this error: TypeError Cannot read properties of undefined (reading 'related') I tried to see if I had mistaken something, but even using the same code used on the tutorial. The error is the same. Here is my code:

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import State from 'App/Enums/States'
import Post from 'App/Models/Post'
import PostStoreValidator from 'App/Validators/PostStoreValidator'

export default class PostsController {
  public async index({ view, auth, params }: HttpContextContract) {
    const page = params.page ?? 1
    const posts = auth.user!.related('posts').query().paginate(page, 20)
    return view.render('studio/posts/index', { posts })
  }

  public async create({ view }: HttpContextContract) {
    return view.render('studio/posts/create', {})
  }

  public async store({ request, response }: HttpContextContract) {
    const data = await request.validate(PostStoreValidator)

    await Post.create({ stateId: State.PUBLIC, ...data })

    return response.redirect().toRoute('studio.posts.index')
  }
}

But there are a lot more, so I am letting my gthub repositore here, so you can take a look at the entire code. The error shows some typeerror, but I have already tried to copy and paste the same code as gave on the tutorial, but nothing changes...

I hope someone can see what can be causing this error because obviously I can't.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!