Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JavaScript / Firestore .set() method - setting an array saved to a variable

Hi, I'm using Firestore's .set() method to create a new document.

To save data in an array format, it needs to be set out like this: arrayExample: [5, true, "hello"]

I have an array of strings saved to a variable (urlsArray) that I want to pass in here, and I have tried all of the following formats:

arrayExample: urlsArray (empty array is saved to Firestore doc - data from urlsArray is not passed in)

arrayExample: [urlsArray] (Firestore error: nested arrays not supported)

arrayExample: urlsArray[0], urlsArray[1], urlsArray[2] (syntax error in text editor after first value)

arrayExample: [urlsArray[0], urlsArray[1], urlsArray[2]] (Firestore error: unsupported field type)

Can anyone suggest how I can pass in the data from my array and save it in the correct Firestore array format?

Here is my code snippet (which currently uploads an empty array to Firestore):

/** 
 * Listens for submit on `#addItemsForm`;
 * Adds new document to Firestore with form input data;
 * Calls addItemToStock() on stock object using Firestore document data.
 */
addItemsForm.addEventListener('submit', function(e){
    e.preventDefault();
    var itemNo = addItemsForm.itemNo.value;
    urlHandler(urlStorageRefHandler(itemNo))
    .then(function(urlsArray){
        console.log(urlsArray);
        //used firestore .set() method to add in data-id 
        //can use firestore .add() method for auto-generated data-id
        firestoreStock.doc(itemNo).set({
            name: addItemsForm.name.value,
            description: addItemsForm.description.value,
            itemNo: addItemsForm.itemNo.value,
            size: addItemsForm.size.value,
            price: Number(addItemsForm.price.value),
            orderTotal: Number(addItemsForm.orderTotal.value),
            imageURLs: urlsArray
        }).then(function() {
            console.log('New doc successfully added to Firestore');
            // clearForm();
            // window.location.reload();
        }).catch(function(reason){
            console.log("Error: " + reason);
        })
    });
});