DEV Community

Discussion on: Building an API with AdonisJS (part 3)

Collapse
 
azeveco profile image
Gabriel Azevedo • Edited

Great tutorial, but I think you should provide more tips for beginners. Ok, you want them to learn, but yould show a little more ou maybe something like "Go and try to do that. If you can't, no worries, down below there's a hint for you".

Anyway, for everyone who's having problems with the method store of the EventController, it's because @nilomiranda tells you to use the following:

const newEvent = await Event.create({...

But he forgot to mention that you need to import the model to the controller. So, below the 'use strict', just import it like this:

const Event = use('App/Models/Event')

@arapl3y said that he was only able to fix the 500 status by changing to:

const newEvent = await user.events().create({...

Yeah, it works, but are at the EventController, so it would be good if the only thing I use is the Event itself to keep things clean. Of course we need to get the authenticated user and pass his id to the creation of the event, but that should be it.

And I strongly recommend you all to use the implementation that @yonglee79 said in his comment. Instead of this:

const userID = auth.user.id

Do that:

const user = await auth.getUser()

And in the place where you create the event, instad of passing the userID variable, simply do that:

const newEvent = await Event.create({
  user_id: user.id,
  title,
  location,
  date,
  time
})

But anyway, great tutorial, @nilomiranda ! And I hope you don''t get angry with my feedback.

Cya!