I've found many issues and solutions to this online but I'm kinda new and tbh I couldn't make it work. It's probably pretty silly but yeah, I need help :)
- The error:
TypeError: Cannot convert undefined or null to object
at Function.assign (<anonymous>)
at organizeCategoryData (C:\Users\alan_\Desktop\melonwallet\utils\records.js:59:35)
at getRecords (C:\Users\alan_\Desktop\melonwallet\controllers\recordController.js:125:30)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
- C:\Users\alan_\Desktop\melonwallet\utils\records.js:59:35
organizeCategoryData(categoryList, amountByCategory) {
const categoryObject = Object.assign( /// LINE 59
...categoryList.map(category => ({
[category.name]: amountByCategory[category._id] || 0
}))
)
categoryList.forEach(category => {
category.amount = categoryObject[category.name]
})
return categoryObject
}
- (C:\Users\alan_\Desktop\ironhack\melonwallet\controllers\recordController.js:125:30)
const categoryObject = organizeCategoryData( //// LINE 125
categoryList,
amountByCategory
)
Thank you so much!
/edit: forgot to clarify this only happens when I try to log-in with a previously created user.
CodePudding user response:
Object.assign only works with objects. ...categoryList.map(...)
is an array. Either put Object.assign within the map (or forEach, since it doesn't return anything). Or take a look at Object.fromEntries.
const categoryObject = {};
categoryList.forEach(category =>
Object.assign(categoryObject, {
[category.name]: amountByCategory[category._id] || 0
})
);
// or
const categoryObject = Object.fromEntries(
categoryList.map(cat => ([cat.name, amountByCategory[cat._id] || 0]))
);
CodePudding user response:
organizeCategoryData(categoryList, amountByCategory) {
const cList = categoryList || []; //This will add a check for null and undefined
const categoryObject = Object.assign( /// LINE 59
...cList.map(category => ({
[category.name]: amountByCategory[category._id] || 0
}))
)
cList.forEach(category => {
category.amount = categoryObject[category.name]
})
return categoryObject
}