updating a column in a table does not work

  Kiến thức lập trình

I’m trying to delete a user from the users array and move it to the deletedusers array and it’s a JSON array in javascript using postgres sql, but I’m trying to add the user with push to the deletedusers array, I logged before and after and the push added the user to the array, but when saving the array with the deleted user replacing the old array without the deleted user, it doesn’t give an error, but it doesn’t do the replacement, in get it returns the array without that user in the array, I just want to replace the old array with the new one that has a new deleted user.

my function:

export async function deletaUsuario(valores) {
  const { id } = valores.pathParameters;
  const cliente = await pool.connect();
  const result = await cliente.query('SELECT * FROM "Users" WHERE id = $1', [
    id,
  ]);
  const user = result.rows[0];
  if (!user) {
    cliente.release();
    throw new Error('Invalid User');
  } else {
    try {
      if (user.deleted === true) {
        throw Error('User alredy deleted');
      } else {
        user.deleted = true;
        await cliente.query(
          'UPDATE "Users" SET "deleted" = $1 WHERE id = $2',
          [user.deleted, user.id],
        );
        const { rows } = await cliente.query(
          `SELECT deletedusers FROM "Deleted"`,
        );
        const deletedusers = rows;
        console.log(deletedusers );
        usuariosdeletados.push(user);
        console.log(deletedusers );
        const usersJSON = JSON.stringify(deletedusers );
        console.log(usersJSON );
        await cliente.query(`UPDATE "Deleted" SET deletedusers = $1`, [
          usersJSON ,
        ]);
        const data = await cliente.query(
          'SELECT deletedusers  FROM "Deleted"',
        );
        console.log('deletedUsers updated: ', data.rows);[here the log is always a [] with nothing inside, the user was not added to the deleted users array]
        await cliente.query('DELETE FROM "Users" WHERE id = $1', [
          user.id,
        ]);
        cliente.release();
        return ['Deleted User', user];
      }
    } catch (error) {
      throw Error(error);
    }
  }
}

the sql I use to create this deletedusers array:

CREATE TABLE IF NOT EXISTS "Deleted" (
    deletedusers JSON DEFAULT '[]'::JSON
  );

I using serverless, alredy set the function on serverless.yml and When I try to delete with a user ID, it doesn’t appear in the deleted users using GET, what did I do wrong? I don’t want to delete the user, I want it to stay in deleted users, so that later my recoverUsers function can recover this user, in a while.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT