Firestore dates written as strings, not Timestamps

Plausible approach, yes, Thank You. Very manual though and error prone as you have to remember to update the firestore query when you update the javascript query. And there is no reuse of firestore queries.

If it could go through some type of transformer first it could remove the manual updates of any field, and only those of Date fields would need to be added/adjusted for. For example:

const lastUpdatedAt = data.recordUpdates.lastUpdatedAt;

return {
  ...data.recordUpdate,
  moment: moment(data.recordUpdates.lastUpdatedAt),
}
1 Like

This solution won't work for an array of dates from a transformer. eg:

I've tried using an external library to 'moment' the dates:
image

When i use the function in the value field of the, which appears correct in the preview box, but writes null to the DB.

image

Are you able to provide a clearer example, I cannot read what is there. Thank you.

Thanks for looking into it, I updated the message above with more details.

Oh, this is a tough one. Based on what you have I would expect item to be an array of map field types which has two elements each named date, with the string value of a date. Since both maps use a key value of date, I am assuming the write fails and that is why it's null (or something like that).

You are trying to write:

item: [
  {
    date: '2022-03-10T17:35:39.165Z'
  },
  {
    date: 'e0ee-03-10T17:35:39.165Z
  }
]

This is invalid for Firestore to store as both map types have the same key value. Try adding a manual document in Firestore and you'll see.

How do you want the data to be in Firestore? We'll start with this and see how we can construct it in Retool so you can get the data written and with Firestore Timestamp types.

Thanks for looking into it. I changed the array to:
image

and the function to
image

The preview and run works:

The DB is updated but with a null for the items.
image

Based on the json value in the preview that looks valid. We would need to see how all the pieces are put together to determine why Firestore is saving a null in this case.

1 Like

Thanks for checking. It appears Retool is converting dates into strings when passed between transformers / scripts, that should be classified as a bug right?

I am pretty sure it is listed already, it is the basis around this entire forum thread. There are some work-arounds listed here, though they are a bit of a pain to deal with.

Hi guys, still struggling with this problem on my side, tell me how dynamically prepare a field with moment? For instance I have a json where names of fields with timestamp could be different, so I iterate them and prepare json, but on my end it does not work throws this: Unexpected token 'F', "Failed to "... is not valid JSON, tell me how could I achieve that?

This is my example:

(function(){
  const initialValue = CollectionItemJSONEditorCreate.value;
  const fieldsToProcessAsTimeStamp = timestampFields.value.split(',');
  const result = Object.keys(initialValue).reduce((aggr, next) => {
    if (fieldsToProcessAsTimeStamp.includes(next)) {
      aggr[next] = moment(initialValue[next]);
    }
    return aggr;
  }, {});
  return {...initialValue, ...result};
})()
}}

Hi @Oleksandr_Tserkovnyi. Where are you running that function at? I am not sure I understand what it's doing as well.

Hello!

Is there a plausible solution for this?

I have a rather simple scenario that is not working.
I have an array with Dates and I want to write them back to firestore. I have tried 3 different approaches:

1:
image
Result:
image

2:
image
Result:


(...)

3:
image

Result:
image

The strange part is that if I do this:
image

it works as I wanted: (although I lost all my data...)
image

Any ideas for this?

Thank you!

The reason 1, 2, & 3 are not working is that you are sending back an array of dates, which are all stringified as they are sent to the Firestore resource in Retool then sent to Firestore as a string. I am not sure about the one that worked as in my experience that would usually end up as a string as well. Perhaps there was a Retool update which handles the moment object in a special manor.

To the best of my knowledge this is no way to handle this other than constructing the array of dates from within the Firestore resource you are using to update firestore as anything passed from a JavaScript query, etc. will be stringified first.

Thank you for the quick answer!

I am actually doing everything directly in the resource that updates firestore:

1 Like

@pedrocarloto you should open this in it's own thread. This topic has been solved already and most probably will not be reviewed by any ReTool support. I myself am not sure why that doesn't work as you described, I would assume it would have as well.

1 Like

@ben is this still an issue in Retool Workflows? I'm consuming a webhook and trying to do two things but dates always land in Firebase as a string.

  1. Convert a mysql datetime string into a Firebase Datetime via {{moment(datetimestring)}}
    2.Set an updatedAt field via {{moment()}}

I've been battling the issue off and on for several weeks. I FEEL like (but could be nuts) that I discovered it working only during a formal RUN but not working when testing via a run of a specific function. I've tried to recreate that nuance but haven't had success. So... one of three things is happening, either:

  1. I'm missing something
  2. There is a bug in Workflows/functions
  3. Firebase looks across the collection to determine the fields type and parses accordingly (a note here... I ran a batch while testing the workflow so now all of my date fields are currently in a string format)

Screenshot 2023-06-02 at 9.31.43 AM

Screenshot 2023-06-02 at 9.31.26 AM

Hey @cjharlin!

Thanks for surfacing this, it's a bug that reproduces on my end as well and I've surfaced it with the Workflows team!

1 Like

Great - is there a place I can track this and other retool bugs?

Changes are posted to updates.retool.com, but for specific bugs, the most public place to check for resolutions or ask for updates at the moment is in these threads.

1 Like

Hey @Kabirdas

Any way to solve this issue on Workflows? :sweat_smile::pray:

I tried to play with this a bit, and it seems that every block stringifies / parses the output...
I tried to do it in the value textbox and it seems that it's a bit less flexible than the value box in the apps... I even went as far as putting in an IIFE to try to overcome this

{{(function() {
  let obj = getObj.data.updateObj
  return {
    ...obj,
    published: false,
    tStart: moment(obj.tStart)
  }
})()}}

but that didn't work either (I assume it also stringifies the obj it receives :man_shrugging:)

Any other things that could work? :pray::crossed_fingers::cherry_blossom:

2 Likes