0

I have been following a Django REST + vue.js tutorial and have encountered a problem in the end.

The task is to POST a new query if the id does not exist, edit otherwise.

The tutorial provides the following code for the purpose:

submitForm(){
  if (this.movie.id === undefined) {
    this.createMovie();
  }
  else {
    this.editMovie();
  }

I have confirmed that both createMovie and editMovie functions work properly, but the query does not enter the createMovie section despite having no data that matches the provided id.

My solution was to introduce a new Boolean variable is_edit, which would turn True after clicking on the edit button, but the solution rendered the problem much more complex and I have no idea to proceed.

That said, is there any solution similar to the one that the tutorial provides?

Just in case, the link to the tutorial is: https://www.youtube.com/watch?v=7GWKv03Osek and for the entire project, https://github.com/Treeboy2762/app

Huge gratitude in advance!

fabien-michel
  • 1,873
  • 19
  • 38
Treeboy
  • 39
  • 5
  • Try out `this.movie.id?this.editMovie():this.createMovie()` – Boussadjra Brahim Aug 17 '20 at 19:05
  • 2
    on first u can use condition without "=" like ``` if (this.movie.id) { this.createMovie(); } else { this.editMovie(); } ``` – Mykyta Aug 17 '20 at 19:06
  • Huge thanks! the problem is solved surprisingly with Mykyta's suggestion. – Treeboy Aug 18 '20 at 01:52
  • I think you'll find this discussion quite interesting too (be sure to read both the first and second answers): https://stackoverflow.com/questions/3390396/how-can-i-check-for-undefined-in-javascript – MTay Aug 19 '20 at 06:52

0 Answers0