How update row in database

Hi,

How its possible to update the row when it already exists in the table please ?

Here is my code :
https://hasteb.in/xawupoti.js
The problem is if the row already exists, i have this error : https://hasteb.in/lulopoji.sql

Thanks :slightly_smiling_face:

Hi👋,

use await when call save()
summoner.save() -> await summoner.save()

To modify the record:

const summoner= await SummonerData.find(<id>)
// Modify the different fields
await summoner.save()

Lucid documentation - find()

Is the problem solved?
Have a good day

Doesn’t work but I found the solution :slight_smile:

   try {
    const summoner = await SummonerData.create({
      name: name
    })
    console.log('Summoner created !')
  } catch (error) {
    const summonerAlreadyRegistered = await SummonerData.findBy('name', name)
    summonerAlreadyRegistered.name = data.name
    await summonerAlreadyRegistered.save()
    console.log('Summoner saved !')
  }

I just removed rows in the db but the principe is the same:)

Thanks !

2 Likes

I am really looking forward to this product finished. Fellow summoner here :slight_smile:

Also another suggestion is to put your validation in it’s own file, and make API url dynamic (you don’t want EUW only, right?)

Instead of firing a query to search for a summoner to check if it exists or not, you can use findorcreate
https://adonisjs.com/docs/4.1/lucid#_findorcreate_whereattributes_values

e.g.

try {
    const summoner = await SummoerData.findOrCreate({ name: name });
    console.log('Summoner Created')
} catch (e) {
    console.log(e)
}
1 Like