TypeError 无法读取未定义的属性(读取“相关”)

发布于 2025-01-13 20:29:09 字数 1292 浏览 0 评论 0原文

我正在尝试遵循使用 Adonisjs 创建完整网站的人的教程,几乎所有内容都与教程中显示的完全一样,除了前端有点不同,但结果是相同的。 现在我试图让浏览器中显示的帖子,但是当我尝试时出现此错误: 类型错误无法读取未定义的属性(读取“相关”) 我试图看看我是否弄错了什么,但即使使用教程中使用的相同代码。错误是一样的。这是我的代码:

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')
  }
}

但是还有更多,所以我让我的 gthub 存储库< /a> 在这里,所以你可以看一下整个代码。该错误显示了一些类型错误,但我已经尝试复制并粘贴与教程中给出的相同的代码,但没有任何变化...

我希望有人能够看到导致此错误的原因,因为显然我不能。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文