I'm having a problem with my get function for my express api. When testing it on postman, the request only works when the user inputs an oid in. However what im trying to do should just show the data from the whole table.
Here's my code, ive been trying to get the get to work without the oid's by using some tutorials but they've not been very good at explaining the whole thing.
async function getComments(req) {
let status = 500, data = null;
try {
const oid = req.query.oid;
if (oid
&& oid.length > 0 && oid.length <= 32
&& oid.match(/^[a-z0-9]+$/i)) {
const sql = 'SELECT * FROM products';
const rows = await db.query(sql, [oid]);
if (rows) {
status = 200;
data = {
'oid': oid,
'productName': rows,
'productCondition': rows,
'productDescription': rows,
'productLocation': rows,
};
} else {
status = 204;
}
} else {
status = 400;
}
} catch (e) {
console.error(e);
}
return { status, data };
}
app.get('/demo', async (req, res) => {
const { status, data } = await getComments(req);
res.status(status);
if (data) res.json(data);
else res.end();
})