Importing an array from CSV to Firestore

Viewed 110

I have a simple CSV file with two fields. One is a string and the other is an array being the elements of the name.

"John Citizen", "[John, Citizen]"

When I import this into Firestore the second field is stored as a string and not parsed as an array.

How can I import these fields as String and Array?

    db.collection('people').add({
        name: record.name,
        elements: record.elements
    })
    .then(function(docRef) {
      console.log("Document written with ID: ", docRef.id);
    })
    .catch(function(error) {
      console.error("Error adding document: ", error);
    });
1 Answers

I was having the same problem, I fixed it by converting my "array" to a string (CSV) and then converting it back to an array. I don't know why but my Angular UI code was converting it back to a string when I tried to push the value to Firestore.

Solution:

record.elements = this.elementsStringToArray(record.elements.toString());

private elementsStringToArray(elementsString: String): String[] {
  const elementsArray = elementsString.slice(0, elementsString.length).split(',');
  return elementsArray;
}

Related