appsmithorg / appsmithorg/appsmith
[Bug] Using IIFE as Row object causes weird errors
- Ngôn ngữ chính
- TypeScript
- Star
- 40.9k
- Fork
- 4.8k
- Merge trung bình
- 1 ngày 22 giờ
- Pull request đã merge (30 ngày)
- 45
Mô tả
## Description
Hello. I'm sorry to be the bringer of more bugs, and this one is an "ephermal" bug in that I cannot easily reproduce it, I can only describe what happened.
I have a form where I post data to a google sheet using the _Insert sheet row_ method of the google datasource connector. Originally this was a static JS object referring to fields in the form using Form1.data. However I realised that I wanted to only post data based on fields that were not 0 so I wrote an IIFE with conditions to only include the data that I needed.
Original Row object:
```JavaScript
{{
{
Datum: moment().format("YYYY-MM-DD HH:mm"),
ID: Form1.data.Select_raft,
Typ: "FlyttaTill",
Från: JSON.parse(Form1.data.Select_tray).id,
"Planta 1": Form1.data.SeedText1,
"Leverantör 1": JSON.parse(Select_tray.selectedOptionValue).supplier_1,
"Antal 1": Form1.data.PlantCount1,
"Planta 2": Form1.data.SeedText2,
"Leverantör 2": JSON.parse(Select_tray.selectedOptionValue).supplier_2,
"Antal 2": Form1.data.PlantCount2,
"Planta 3": Form1.data.SeedText3,
"Leverantör 3": JSON.parse(Select_tray.selectedOptionValue).supplier_3,
"Antal 3": Form1.data.PlantCount3,
"Planta 4": Form1.data.SeedText4,
"Leverantör 4": JSON.parse(Select_tray.selectedOptionValue).supplier_4,
"Antal 4": Form1.data.PlantCount4,
}
}}
```
New, improved IIFE:
```JavaScript
{{(
function move_to_event_old() {
let data = {
Datum: moment().format("YYYY-MM-DD HH:mm"),
ID: Form1.data?.Select_raft,
Typ: "FlyttaTill",
Från: JSON.parse(Form1.data.Select_tray)?.id,
};
if (parseInt(Form1.data?.PlantCount1 || 0) > 0) {
data = {
...data,
"Planta 1": Form1.data.SeedText1,
"Leverantör 1": JSON.parse(Select_tray.selectedOptionValue).supplier_1,
"Antal 1": Form1.data.PlantCount1,
};
}
if (parseInt(Form1.data?.PlantCount2 || 0) > 0) {
data = {
...data,
"Planta 2": Form1.data.SeedText2,
"Leverantör 2": JSON.parse(Select_tray.selectedOptionValue).supplier_2,
"Antal 2": Form1.data.PlantCount2,
};
}
if (parseInt(Form1.data?.PlantCount3 || 0) > 0) {
data = {
...data,
"Planta 3": Form1.data.SeedText3,
"Leverantör 3": JSON.parse(Select_tray.selectedOptionValue).supplier_3,
"Antal 3": Form1.data.PlantCount3,
};
}
if (parseInt(Form1.data?.PlantCount4 || 0) > 0) {
data = {
...data,
"Planta 4": Form1.data.SeedText4,
"Leverantör 4": JSON.parse(Select_tray.selectedOptionValue).supplier_4,
"Antal 4": Form1.data.PlantCount4,
};
}
return data;
}
)()}}
```
This resulted in an error message referring to a variable "n" that didn't exist in my script, and a circular dependency error too. On top of that the selects on the form stopped working, they seemingly couldn't use their queries to the google sheet. But only in edit mode! When I deployed everthing worked as expected, posting only the data I needed.
What I then did was to create a copy of the now bugged datasource, the original name was `move_to_event` so the copy became `move_to_eventCopy`. I then put back the original JS object in the Row object field of `move_to_event` and page went back to working as before. I renamed `move_to_event` to `move_to_event_old` and `move_to_eventCopy` to `move_to_event` and lo and behold, after changing the reference in the onClick handler of the submit button to use `move_to_event` the script didn't generate any errors.
### Steps to reproduce the behaviour:
Sadly this is very hard to reproduce...
### Important Details
I cannot of course say what the cause of this is, but I have two thoughts at least.
My initial suspicion about the cause of this is that it's somehow connected to my use of _åöä_ in the field names of the Google sheet and thus in the scripts. There was an earlier bug with populating the Table widget with rows having non-ascii characters that I reported some time ago.
However, while writing this issue I messed around some more and realised that the row `Från: JSON.parse(Form1.data.Select_tray)?.id,` in the IIFE should be
` Från: JSON.parse(Form1.data?.Select_tray||"{}")?.id,` so this might also be the reaon, but it's weird that the main issue, the selects of the form not working in edit mode were resolved by my copy paste dance before I made this change.
While the functionality of the actual app was never broken, the form worked as intended when deployed after all, it is problematic since it seems there are ways that one data source can affect others to the detriment of the whole form when in edit mode. This makes it really hard to debug "my errors", i.e. the "normal" bugs one introduces while working with scripts on a page.
Hope this might provide some help, these kinds of error must be hard to nail down.
- Version: Cloud
- Browser FireFox
- OS: Linux mint 19
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.