I have a fetch request to delete some lists from my backend, but the console is giving me a complaint. The console says “Uncaught (in promise).”
This is my fetch() in the frontend:
const handleClickGarbage = (key) => { // for deleting a grocery list
const temp = loginUserr;
try {
console.log('inside click garbage');
const accessToken = temp.accessToken;
console.log(accessToken);
const param = key;
console.log(param);
fetch(`http://localhost:3010/v0/grocerylists?listName=${param}`, {
method: 'DELETE',
headers: new Headers({
'Authorization': 'Bearer ' + accessToken,
}),
})
.then((results) => {
if (!results.ok) {
throw results;
}
console.log(results);
getCurrentGroceryListTabInfo(); // get the current tab info again because we just updated the info
});
} catch (e) {
console.log(e);
}
};
This is my user.js:
exports.deleteGroceryList = async (req, res) => {
const listNamee = req.query.listName;
const memberIdd = req.userID;
console.log('inside delete gl');
console.log(listNamee);
console.log(memberIdd);
const deleted = await db.deleteGroceryList(listNamee, memberIdd);
console.log('user.js line 286)n');
console.log(deleted);
if (deleted === null) {
res.status(400).send();
} else {
console.log('user.js line 292)n');
res.sendStatus(200);
}
};
This is my db.js:
exports.deleteGroceryList = async (listNamee, memberIdd) => {
const listName = listNamee;
const memberId = memberIdd;
const select = 'DELETE FROM grocery_list WHERE list_name = $1 AND member_id = $2 RETURNING *';
const query = {
text: select,
values: [listName, memberId],
};
const {rows} = await pool.query(query);
console.log('db.js line 495)n');
console.log(rows);
if (rows.length > 0) {
return rows.length;
} else {
return null;
}
};
And this is my openapi.yaml:
/grocerylists:
delete:
description: Deletes a grocery list from user's existing grocery lists'
security:
- bearerAuth: []
parameters:
- name: listName
in: query
description: name of grocery list to delete
schema:
type: string
responses:
200:
description: Removed list from grocery lists successfully
401:
description: Unauthorised
400:
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
I suspect it is something wrong with my api because console.logging results in the front end shows that user.js returned a status code of 200.: