1

I can't understand why this is happening I have added sample data manually and then I am pushing some more data later. Ultimately I want to display the data in a flatlist. Please help me, This is very important for my project. Thank yo Code:

class TasksScreen extends Component {
constructor(props) {
  super(props);

  this.state = {
    tasks: [],
  };
  var tempTasks = [
    {
      key: 'abc',
      val: 'abc',
    },
  ];
  var that = this;

  var taskRef = database()
    .ref('/Tasks/' + auth().currentUser.uid)
    .on('value', dataSnapshot => {
      var key = dataSnapshot.key;
      console.log('UID KEY: ' + key);
      dataSnapshot.forEach(childSnaps => {
        var key = childSnaps.key;
        console.log('TASKID KEY: ' + key);
        childSnaps.forEach(taskData => {
          var taskKey = taskData.key;
          var taskVal = taskData.val();
          console.log('taskData KEY: ' + taskKey);
          console.log('taskData VAL: ' + taskVal);
          console.log('taskData.val().title: ' + taskVal);
          tempTasks.push({
            taskTitle: 'title',
            taskDescription: 'description',
            taskDueTime: 'time',
            taskDuedate: 'date',
          });
        });
      });
      console.log('tempTasks: ' + tempTasks);
      that.setState({tasks: tempTasks});
      console.log('STATE TASKS: ' + that.state.tasks);
    });
}

enter image description here

yashatreya
  • 782
  • 1
  • 9
  • 32
  • 3
    `"" + {}` will always result as `[object object]` so instead of concatenating values, you should pass them as separate argument, i,e `console.log('str', object)` – Code Maniac Oct 22 '19 at 16:56

2 Answers2

1

"" + {} will always result as [object object] ( Because of string concatenation ) so instead of concatenating values, you should pass them as separate argument, i,e console.log('str', object), if you need it as a single concatenated string you can use JSON.stringify and then concat them

let obj = {
  a: '123',
}

console.log("String" + obj)
console.log("String", obj)
console.log("String" + JSON.stringify(obj))
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

Since you are concatening string, the object's toString method is called, which returns '[Object object]'. You could replace your + with a coma , to evaluate each variable in your console.log individually. :

let object = {
    title: 'test1',
    count: 10
}

// concatenation
console.log('object : ' + object);

// individual evaluation.
console.log('object : ', object);
Nicolas
  • 8,077
  • 4
  • 21
  • 51