I’m having some trouble getting some values from my database via express. On my server, I have this router call:
router.get('/list/:id', TokenController.isAValidToken, async (req, res) => {
const { cardID } = req.query;
try {
if (cardID != null) {
...
} else {
const cards = await CardsController.getCardsForList(req.params.id);
const orderItems = await OrderItemController.getOrderItemsForCardsInList(cards, req.params.id);
console.log(orderItems); // <- This prints an array with the info I need
res.json(orderItems);
}
} catch (error) {
...
}
});
My getOrderItemsForCardsInList function has this:
static async getOrderItemForCardInList(cardID, listID) {
return OrderItem.find({ card: cardID, list: listID }).exec();
}
static async getOrderItemsForCardsInList(cards, listID) {
return Promise.all(cards.map((card) => OrderItemController.getOrderItemForCardInList(card, listID)));
}
On my client, I call the router function like this:
async function getOrderItemsForCardsInList(listID) {
const token = await getToken();
const response = await fetch(`${basePath}/order/list/${listID}`, {
method: 'GET',
headers: getTokenAuthHeaders(token)
});
return response.json();
}
export default function useOrderItemsForCardsInList(cards) {
const { listID } = useParams();
return useQuery(['order', ...cards], () => {
if (listID != null) {
return getOrderItemsForCardsInList(listID);
} else {
throw Error('No list id defined');
}
});
}
And I call that custom hook over here:
const { orderdata } = useOrderItemsForCardsInList(cards.map((card) => card !== null && card._id));
console.log('Data', orderdata);
The problem is that orderdata
is printing undefined
, meaning that the values are not getting returned as they should, my theory is that probably I’m messing something up in my useOrderItemsForCardsInList
custom hook.