Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: Cannot convert undefined or null to object: point me in the right direction

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]))
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading