K
K
komigor2021-08-16 18:21:41
PostgreSQL
komigor, 2021-08-16 18:21:41

Why are fields added to the database incorrectly?

for(let client of clients) {
      const phone = client.PHONE ? client.PHONE[0].VALUE : null; 
      const email = client.EMAIL ? client.EMAIL[0].VALUE : null;
      console.log(clients)
      console.log(phone);
      const result = await knex("clients").insert({firs_name: client.NAME,
                                    last_name: client.LAST_NAME,
                                    patronymic: client.SECOND_NAME, 
                                    role: null,
                                    //phones: knex.raw('array_append(phones, ?)', [phone]),
                                    //emails: knex.raw('array_append(emails, ?)', [email])
                                  })
        await knex("clients").update({phones: knex.raw('array_append(phones, ?)', [phone])}) 
        await knex("clients").update({emails: knex.raw('array_append(emails, ?)', [email])});

And in the table I get that all phones are recorded in the first object, two in the second and one in the third
1 | Maxim | sddfs | dsfsd | {212353245345,+3812175673035,+28121675673037} |
{[email protected],NULL,NULL} |
2 | Taras | Gvymvm | {21321135673035,+380675673037} | {NULL, NULL} |
3 | Volodymyr | yvmyvmyv | | {+7231230675673037}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
komigor, 2021-08-16
@komigor

We need to add indexes await knex("clients").where({id: client.ID }).update({phones: knex.raw('array_append(phones, ?)', [phone])})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question