title: Returning dates / server timestamps in cloud callable functions
body: <p>I'm using a callable function from a client app to retrieve some data from firestore. The data is created using:</p>
  
  <pre><code>projectData = {
    created: firebase.firestore.FieldValue.serverTimestamp(),
    ...otherData
  }
  firebase.firestore().collection(projectsCollection).add(projectData)
  </code></pre>
  
  <p>And I can see the timestamp is correctly saved in the firestore console. The callable function does some other things and has error handling, but the data retrieval is done like this (using lodash to expand each document into an object to return to the client):</p>
  
  <pre><code>const projectRef = firestore.collection(gProjectsCollection).orderBy('created')
  return projectRef.get().then(snapshot =&gt; {
    return {
      projects: _.chain(snapshot.docs)
        .keyBy('id')
        .mapValues(s =&gt; s.data())
        .value()
    }
  })
  </code></pre>
  
  <p>This mostly works, but the returned object has a mangled <code>created</code> property (shown here in functions shell output):</p>
  
  <pre><code>RESPONSE RECEIVED FROM FUNCTION: 200, {
    "result": {
       "projects": {
        "XcRyQyaxLguRdbNmxQdn": {
          "name": "c",
          "description": "c",
          "created": {
            "_seconds": 1543405587,
            "_nanoseconds": 169000000
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>I'm presuming this is because the callable function uses <code>JSON.stringify()</code> internally, and the server timestamp isn't converted correctly. I tried explicitly converting the timestamp to a date like this:</p>
  
  <pre><code>return projectRef.get().then(snapshot =&gt; {
    return {
      exVersion,
      exId,
      projects: _.chain(snapshot.docs)
        .keyBy('id')
        .mapValues(s =&gt; s.data())
        .forEach(d =&gt; { d.created = d.created.toDate() })
        .value()
    }
  })
  </code></pre>
  
  <p>but now I get an empty object back:</p>
  
  <pre><code>RESPONSE RECEIVED FROM FUNCTION: 200, {
    "result": {
      "projects": {
        "XcRyQyaxLguRdbNmxQdn": {
          "name": "c",
          "description": "c",
          "created": {}
        }
      }
    }
  }
  </code></pre>
  
  <p>I suspect the real problem here is that callable functions aren't set up to return date objects. If I go one more step and convert the timestamp into a string, I finally get something back in the client:</p>
  
  <pre><code>return projectRef.get().then(snapshot =&gt; {
    return {
      exVersion,
      exId,
      projects: _.chain(snapshot.docs)
        .keyBy('id')
        .mapValues(s =&gt; s.data())
        .forEach(d =&gt; { d.created = d.created.toDate().toISOString() })
        .value()
    }
  })
  </code></pre>
  
  <p>gives:</p>
  
  <pre><code>RESPONSE RECEIVED FROM FUNCTION: 200, {
    "result": {
      "exVersion": 9,
      "exId": null,
      "projects": {
        "XcRyQyaxLguRdbNmxQdn": {
          "name": "c",
          "description": "c",
          "created": "2018-11-28T11:46:27.169Z"
        }
      }
    }
  }
  </code></pre>
  
  <p>I couldn't find anything in the documentation for callable functions about special handling for dates, but am I missing something in how this should be done?</p>
  
  <hr>
  
  <p>Some further analysis in the callable function suggests <code>JSON.stringify()</code> is involved, but isn't the whole problem:</p>
  
  <pre><code>console.log(JSON.stringify(d.created)):
  info: {"_seconds":1543405587,"_nanoseconds":169000000}
  JSON.stringify(d.created.toDate())
  into: "2018-11-28T11:46:27.169Z"
  </code></pre>
  
  <p>So calling <code>d.created.toDate()</code> <em>should</em> be sufficient. But I've had other cases where <code>Date</code> objects are just not returned by callable functions, e.g.:</p>
  
  <pre><code>const testObject = {
    some: 'thing',
    d: new Date()
  }
  
  console.log('JSON:', JSON.stringify(testObject))
  
    return projectRef.get().then(snapshot =&gt; {
      return {
        testObject,
        projects: _.chain(snapshot.docs)
          .keyBy('id')
          .mapValues(s =&gt; s.data())
          .forEach(d =&gt; { console.log('d.created is:', JSON.stringify(d.created.toDate())); d.created = d.created.toDate() })
          .value()
      }
    })
  </code></pre>
  
  <p>Note in the output below, the logged results of <code>JSON.stringify()</code> on date objects seems to work, but when those objects are contained within the object returned from the function, they both come out as empty objects:</p>
  
  <pre><code>firebase &gt; getAllProjects({uid: 1234})
  Sent request to function.
  firebase &gt; info: User function triggered, starting execution
  info: JSON: {"some":"thing","d":"2018-11-28T13:22:36.841Z"}
  info: Got 1 projects
  info: d.created is: "2018-11-28T11:46:27.169Z"
  info: Execution took 927 ms, user function completed successfully
  
  RESPONSE RECEIVED FROM FUNCTION: 200, {
    "result": {
      "testObject": {
        "some": "thing",
        "d": {}
      },
      "projects": {
        "XcRyQyaxLguRdbNmxQdn": {
          "description": "c",
          "created": {},
          "name": "c"
        }
      }
    }
  }
  </code></pre>