Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgresql db.run command does not return id. #164

Open
7 tasks done
vjocw opened this issue Feb 1, 2019 · 0 comments
Open
7 tasks done

Postgresql db.run command does not return id. #164

vjocw opened this issue Feb 1, 2019 · 0 comments

Comments

@vjocw
Copy link

vjocw commented Feb 1, 2019

  • System Information

    • OS type and version: Mac OS X 10.14.2.
    • Node version: v8.11.2
    • Any error messages that may be in the console where you ran npm start: no errors
    • Any error messages in the JS console: none in Chrome.
  • Describe the bug

I'm using Postgresql. On the update order function I have it so:

export async function updateOrder(id, data, details = []) {
  const db = await getDb();

  await db.run('BEGIN;');
  try {
    const updateStatement = sql`
    UPDATE CustomerOrder
    SET employeeid=$1, customerid=$2, shipcity=$3, shipaddress=$4, shipname=$5, shipvia=$6, shipregion=$7, shipcountry=$8, shippostalcode=$9, requireddate=$10, freight=$11
    WHERE id = $12`;
    const result = await db.run(updateStatement, data.employeeid, data.customerid, data.shipcity, data.shipaddress, data.shipname, data.shipvia, data.shipregion, data.shipcountry, data.shippostalcode, data.requireddate, data.freight, id);
    console.log('result is ', result);
    await Promise.all(details.map((detail) => {
      return db.run(sql`
        UPDATE OrderDetail 
        SET productid=$1, quantity=$2, unitprice=$3, discount=$4
        WHERE id = $5
      `, detail.productid, detail.quantity, detail.unitprice, detail.discount, detail.id);
    }))
    await db.run('COMMIT;');
    return { id: result ? result.lastID : undefined };
  } catch (e) {
    await db.run('ROLLBACK;');
    throw e;
  }
}

When I'm inside of the edit page, such as /orders/10351/edit, and hit save order, it successfully makes a POST request. And the update is successful. See below:

image

But afterwards, when it tries to refresh the page with the id that it got back from the POST request, it fails because the db.run command does not return an id. See below.

image

My tries at solving this problem was to return the res object in mysql-db.ts code snippet below:

   return this.measure(q, params, async () => {
      let [res, _] = await this.connection.query(q, params);
      if (res && typeof (res as any).insertId !== 'undefined') {
        let lastID = (res as any).insertId;
        return { lastID, res };
      }
      return { res };
    });

But I could not get the object back for some reason.

Another thing I tried was to rebuild the database and table. That did not solve the problem.

I'm really stuck here trying to solve this problem. I would like this to work. Can you help?.. Thank you.

  • Expected behavior
    result variable should return me the id.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant