Arrays in Firestore

Maybe there is something that I don’t get to do right, but I don’t know how to save arrays from RS in Firestore. How would I select the kind of data that I am introducing into Firestore other that a string?

You need to use repeating keys with different values on firestore for it to load as an ‘array’ on react studio (i.e. a column)

Thank you for your answer but I don’t quite understand what you mean. Could you elaborate a bit what you refer with “repeating keys”? Thank you in advance.

Do you mind sharing an example of the data you’re trying to work with? I can help better

Of course!

I have created a component to input data into Firestore. Thing is that RS by default save every value as a string. In order to save a date in a number value I got some help to customize a small script like in the screenshot:

I am guessing that I should also use the script to save several values as an array in the format that Firestore accepts (example in categories):

When I have tried to save using brackets and commas, it is saved as a string with those symbols and not as a string.

It would be great if it would be the other way too. When I have an array in Firestore and I populate the data, it appears with the brackets and I guess there would be a way to customize the output of that array.
Captura de pantalla 2021-07-08 a las 9.05.01

Thank you in advance.

I think I kind of follow what you’re trying to do.

If you need to upload more than just plain strings to firebase (e.g. number, dictionaries or maps like your category example) I would suggest going for the ‘script’ interaction in RS instead of the ‘save data’ interaction.

There are functions that are available throughout the project to help you add data to firebase. For example:

If you wanted to add a new document on firebase, you’d do something like the following:

var row = {
    artist: 'xyz',
    categories: {
        0: 'Facil',
        1: 'Film/TV'

this.props.appActions.addToDataSheet('exampleSheetName', row);

There are a couple of other functions like this: .appActions.updateInDataSheet ; .appActions.removeFromDataSheet.

You can also get more information on what you can put into that ‘row’ variable here.

Hopefully that helps!

I am back into this and I completely understand how to achieve the array when creating the document. But how can I do it to update an array?

Let’s say that I already have a document with an array and I want to add or remove one element. In Firestore documentation they have the arrayUnion and arrayRemove but I don’t know if that is what I have to do in React Studio as in the example it is used the addToDataSheet.

Thank you!

Hi. Yes, if you want to add or remove elements from an Array in Firestore, that’s what you should do.

Example. Like or favorite button.

React Studio
Just after you add the data to a sheet ‘collection’, next using the script option {…}
var db = firebase.firestore();

let doc = this.props.document_key;
this.props.appActions.updateDataSlot(‘ds_SlotThreadID’, doc);

let ThreadId = this.props.appActions.dataSlots[‘ds_SlotThreadID’]
let uid = this.props.appActions.dataSlots[‘ds_SlotUserID’];

const docRef = db.collection(Threads).doc(${ThreadId})
// Atomically add a userId to the “likes” array field.
docRef.update({likes: firebase.firestore.FieldValue.arrayUnion({uid})

Best, Marcos