Saturday, April 11, 2020

Error: Objects are not valid as a React child



Error Blog 

This blog is about the error Error: Objects are not valid as a React child 

Problem Statement

error on start of reactjs application

Error Details 

error Log

ERROR DETAILS 

Error: Objects are not valid as a React child (found: object with keys {TASK_ID, TASK_DESC, TASK_REF, TASK_STATUS, UPDATED_ON}). If you meant to render a collection of children, use an array instead. in div (at task-list.js:22) in TaskList (at App.js:10) in div (at App.js:9) in App (at src/index.js:9) in StrictMode (at src/index.js:8)



Error Code 

You will face this error if you try to convert an array response to json .



router.get('/', function (req, res) { Task.getAllTask(function (err, task) { console.log('controller') if (err) res.send(err); console.log('res', task); res.send( task); }); });





  

Solution

change the code so that the array is handled .

Code Changes 

wrap the array inside a json .

  
        
    
router.get('/', function (req, res) { Task.getAllTask(function (err, task) { console.log('controller') if (err) res.send(err); console.log('res', task); res.send({'resp':task}); }); });

         

OUTPUT
works 



Thanks for reading . Let me know your thoughts in the comments below    .

No comments:

Post a Comment

ec2-user@ec2 Permission denied