3
function querydb(){
  cursor = dbConnection.collection("logs").find(queryObj).limit(valueList[1]);

  /*Either decide here to foreach for file or for console OR...*/
  cursor.forEach(function(item){
      /*Decide here whether to write to file or console*/
      if(valueList[0] != null){
        fs.writeFile(valueList[0], item, 'utf-8', console.log("finished!!"));
      }
      if(valueList[0] == null){
        console.log(item);
        console.log("Done");
      }

      /*CHECK TO SEE IF THE CURSOR IS EXHAUSTED*/
      if(cursor == null){
        //now we can close the database
        dbConnection.close();
      }
    }
  );
};

Here is my querydb function. As you can see i check if the value is not equal to null and if its true i call writeFile. but in the .txt it just prints "object object". Any idea on whats going on??

Hunter Turner
  • 6,804
  • 11
  • 41
  • 56
  • It's just going to call `.toString()` on your object. You can use `JSON.stringify()` to dump the object as JSON or else write your own code to render the object as a string according to your needs. – Pointy Mar 03 '16 at 22:11
  • i checked how to use stringify but its not that detailed on its description. can you tell me how to use it?? – Hanad Hanad Mar 03 '16 at 22:14
  • @Pointy i think you should present your comment as an actual answer. Because for me it's the correct answer to the question. – yeiniel Mar 03 '16 at 22:15
  • No its not because im still lost. so can someone tell me what my program is doing for that issue?? – Hanad Hanad Mar 03 '16 at 22:17

1 Answers1

3

The writeFile signature is:

fs.writeFile(file, data[, options], callback)

so, the data you are writing must be an object. Which is why Object Object is being written to the file. Try deserializing the "item" parameter like:

 fs.writeFile(valueList[0], JSON.stringify(item), 'utf-8', console.log("finished!!"));