Pass buggy code to Cerewro and get the detected bug, root cause explained in plain language and fixed code. Also refactor complex functions, add TypeScript typing, improve performance and generate tests automatically.
This JavaScript gives "TypeError: Cannot read properties of undefined (reading 'map')". Analyze the code, explain the cause and fix it: [paste code]
// ❌ Before: nested callbacks
getUser(id, (err, user) => {
if (err) return handleError(err);
getOrders(user.id, (err, orders) => { ... });
});
// ✅ After: async/await
async function loadUserData(id) {
try {
const user = await getUser(id);
const orders = await getOrders(user.id);
renderPage(orders);
} catch (err) {
handleError(err);
}
}
Generate complete Jest tests for this function. Include: normal input, empty input, edge cases, wrong types and expected errors: [paste function]