felixmp
felixmp

Reputation: 350

Firestore finds undefined value in fully defined JSON, key does not even exist

I am trying to upload a JSON-object to Google Firestore.

When setting the Object to a Firestore Document my code throws the following Error:

Error: Value for argument "data" is not a valid Firestore document. 
Cannot use "undefined" as a Firestore value 
(found in field audit.`20`.requests.`0`.lrEndTimeDeltaMs).

Now the first thing I did was log the Object right before the upload to check for undefined values:

console.log(JSON.stringify(resultsToUpload));

return database
  .collection("foo1")
  .doc("bar1")
  .set(resultsToUpload);

Not only are all values defined in the object, but the mentioned field audit.`20`.requests.`0`.lrEndTimeDeltaMs does not even exist:

resultsToUpload = {  
  "audit":{   
    "20":{  
      "ronaldScore":3,
      "id":"network-requests",
      "requests":[  
        {  
          "url":"https://www.example.com/",
          "startTime":0,
          "endTime":62.16699999640696,
          "transferSize":15794,
          "resourceSize":78243,
          "statusCode":200,
          "mimeType":"text/html",
          "resourceType":"Document"
        }
      ]
    }
  }
}

The data comes from a Google Lighthouse Audit.

Calculating the UTF-8 string length of the stringified JSON objects results in a size of 30 MB.

1) All values are defined (some are null, which should not be a problem).

2) The mentioned field does not even exist in the JSON.

My question is: How can this happen? How can a field just appear? Also: How would I fix this issue?

Upvotes: 0

Views: 1673

Answers (1)

felixmp
felixmp

Reputation: 350

using JSON.stringify on a Javascript-Object hides all "undefined" values (as well as their keys) and then stringifies what is left.

That is because JSON does not have such a thing as "undefined".

So the log showed a fully defined JSON, even though the actual JS-Object did contain "undefined" values.

Upvotes: 7

Related Questions