My API is throwing an SQL error and not sending a response, despite passing all the correct inputs as far as I can tell.
This is how I call the database:
const fetchData: any = async (po: any) => {
const response: any = await axios.get(`/api/bulk-email/${po}`);
//Data fetched...
};
Here's the query I'm using in my API to interact with the database:
`SELECT o.channel_order_id, oi.po, oi.id, p.product_id, oi.quantity, oi.scanned_qty,
oi.qty_canceled, oi.channel_order_item_id, p.description, p.manufacturers_id, p.gtin, p.upc
FROM shopper s INNER JOIN orders o on s.id = o.shopper_id INNER JOIN orderitems oi ON o.id = oi.order_id
INNER JOIN products p ON oi.item_id = p.id
WHERE oi.po = ? AND (oi.quantity > oi.scanned_qty or oi.scanned_qty is null);`,
This is where I set the value of PO:
const pullPOs = async () => {
const response: any = await axios.get(`/api/get-pos`);
setPOs(response.data);
};
const handleSelectPO = (e: any) => {
setPo(e.target.innerText);
fetchData(e.target.innerText);
};
When I console.log(po);
, it returns the expected result.
When I
console.log(fetchData(e.target.innerText));
, I get a pending promise. Likely due to the unresolved SQL error.
Here's the specific error message:
SqlError: Parameter at position 1 is undefined
SELECT o.channel_order_id, oi.po, oi.id, p.product_id, oi.quantity, oi.scanned_qty,
oi.qty_canceled, oi.channel_order_item_id, p.description, p.manufacturers_id, p.gtin, p.upc
FROM shopper s INNER JOIN orders o on s.id = o.shopper_i...
...
API resolved without sending a response for /api/bulk-email/OT_3_5_TEST, this may result in stalled requests.
This is the code snippet for the API:
import type { NextApiRequest, NextApiResponse } from 'next';
import * as pool from '../../../src/utils/dbConnection';
import console from 'console';
...
Thank you for any assistance provided!