Skip to content

Instantly share code, notes, and snippets.

@churchthecat
Last active April 6, 2020 08:28
Show Gist options
  • Save churchthecat/d69c0524c4f26578b66ab9fded95ee3d to your computer and use it in GitHub Desktop.
Save churchthecat/d69c0524c4f26578b66ab9fded95ee3d to your computer and use it in GitHub Desktop.
async function addToCart(orderid) {
let sql = `CALL add_to_cart(?);`;
let res2;
res2 = await db.query(sql, [orderid]);
console.log("addtocart", res2);
console.info(`SQL: ${sql} got ${res2.length} rows.`);
return res2[0];
}
async function selectOrder(orderid, bestalltantal) {
let sql = `CALL select_order(?, ?);`;
let res1;
res1 = await db.query(sql[orderid, bestalltantal]);
console.log(res1);
console.log("selectOrder");
console.info(`SQL: ${sql} got ${res1.length} rows.`);
return res1;
}
route
router.post("/order", urlencodedParser, async (req, res) => {
console.log(JSON.stringify(req.body, null, 4));
console.log("routr post1");
let data;
data.res1 = await eshop.selectOrder(1, "0,0,0,1,0,0,0,0,1");
console.log(data.res1);
// data.res2 = await eshop.addToCart(1);
// console.log(data.res2);
// res1.redirect(`/eshop/order`);
console.log("routr post2");
});
DDL
DROP PROCEDURE IF EXISTS select_order;
DELIMITER ;;
CREATE PROCEDURE select_order(
a_orderid SMALLINT,
a_antalbestallt VARCHAR(255)
)
BEGIN
DECLARE str VARCHAR(255) DEFAULT '';
DECLARE produktidantal INT DEFAULT 0;
DECLARE a_kundid INT;
DECLARE a_pris INT;
DECLARE a_produktnamn VARCHAR(20);
DECLARE a_orderrader INT;
-- DECLARE a_produktidlager INT;
DECLARE x INT DEFAULT 0;
DECLARE y INT DEFAULT 0;
DECLARE element1 VARCHAR(150);
DECLARE element2 INT;
SET produktidantal = (SELECT COUNT(produktid) FROM produkt);
SET x = 1;
SET y = 0;
SET a_kundid = (SELECT kundid FROM ordrar WHERE a_orderid = orderid);
-- SET a_produktidlager = (SELECT produktid FROM produkt WHERE a_orderid = produktid);
WHILE x <= produktidantal DO
-- IF a_antalbestallt != 0;
SET element1 = SUBSTRING_INDEX(a_antalbestallt, ',', + x);
SET element2 = SUBSTRING(element1, - 1);
-- SET str = CONCAT(str, x, ':', element1, ',');
SET x = x + 1;
SET y = y + 1;
SET a_produktnamn = (SELECT produktnamn FROM produkt WHERE y = produktid);
SET a_pris = (SELECT pris FROM produkt WHERE y = produktid);
SET a_orderrader = (SELECT COUNT(orderrader) FROM orderrader);
-- select a_orderrader;
IF element2 != 0 THEN
// INSERT INTO temp (`kundid`, `orderid`, `produktid`, `produktidlager`, `antalbestallt`)
// VALUES
// (a_kundid, a_orderid, y, y, element2)
// ;
INSERT INTO orderrader (`kundid`, `orderid`, `produktid`, `antalbestallt`, `produktnamn`, `pris`)
VALUES
(a_kundid, a_orderid, y, element2, a_produktnamn, a_pris)
;
END IF;
END WHILE;
SELECT * FROM orderrader;
END
;;
DELIMITER ;
FEL
(node:8456) UnhandledPromiseRejectionWarning: Error: ER_EMPTY_QUERY: Query was empty
at Query.Sequence._packetToError (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/protocol/sequences/Query.js:79:18)
at Protocol._parsePacket (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/Connection.js:526:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:607:20)
--------------------
at Protocol._enqueue (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/mysql/lib/Connection.js:198:25)
at /home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/promise-mysql/lib/helper.js:16:24
at Promise._execute (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/bluebird/js/release/debuggability.js:384:9)
at Promise._resolveFromExecutor (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/bluebird/js/release/promise.js:518:18)
at new Promise (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/bluebird/js/release/promise.js:103:10)
at Connection.promiseCallback (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/promise-mysql/lib/helper.js:6:10)
at connection.query (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/promise-mysql/lib/connection.js:64:28)
at Object.selectOrder (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/src/eshop.js:115:21)
at router.post (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/route/eshop.js:175:29)
at Layer.handle [as handle_request] (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/express/lib/router/layer.js:95:5)
at next (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/express/lib/router/route.js:137:13)
at /home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/body-parser/lib/read.js:130:5
at invokeCallback (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/raw-body/index.js:224:16)
at done (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/home/anders/BTH/Dbwebbkurser/databas/me/kmom06/eshop2/node_modules/raw-body/index.js:273:7)
(node:8456) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment