faber-finances/server/api/transactions/index.get.js

23 lines
No EOL
682 B
JavaScript

import { getCollection } from '../../utils/db.js'
export default defineEventHandler(async (event) => {
try {
const collection = await getCollection('transactions')
const transactions = await collection.find({}).toArray()
// Convert MongoDB _id to id for frontend compatibility
const formattedTransactions = transactions.map(transaction => ({
...transaction,
id: transaction._id.toString(),
_id: undefined
}))
return formattedTransactions
} catch (error) {
console.error('Error fetching transactions:', error)
throw createError({
statusCode: 500,
statusMessage: 'Failed to fetch transactions'
})
}
})