TypeError 无法读取未定义的属性(读取“相关”)
我正在尝试遵循使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论