sync issue

Hi,
site: https://www.jobs.abrahamhostels.com/
page: https://www.jobs.abrahamhostels.com/ hr

issue:
The code adds JobNum from Jobs collection, as long as it is unique as jobId in Images collection.
Somehow, I get duplicates!
I know this code is simple, but something is wrong.
The line if (length === 0) is always true.

Thanks a lot!

first of all you don’t need an async in your function. if (length === 0) that means you query is wrong. Could you share you collection structure? :slight_smile:

Hi, The collections types are fine . If I put some items in the collection, the length is not 0.
The issue is a sync issue.
It iccures when the query happens before the insert, and this is why the length is 0.
Both compared fields are ‘Text’ type, and the values from Jobs/jobNum is the one that inserted to Images/jobId only by this function, so they must be the same.
Thanks anyway:)
Dafna

I ve never spoke of type here :s

I just want to see you data structure to better understand your problem. If there is a sync issue you need to share the insert code as well. you can have a look at collection hooks maybe that would help

Hi, you can see the query and the insert above in the code image. Thanks

ok from what I read in your code, it will ALWAYS query Images then insert image . That because the insert is inside the query “then”.

Yup. It checks if the value exists in Images, and should insert only if it’s not.

If you have duplicate maybe it’s because you call the addToImage 2 times in a row in your code.

so the first query run then the second query run then the first insert run then the second insert run.

Maybe that’s how you end up with duplicate.

The duplicates are in the Jobs collection, and it’s ok.
When running with this function, I wish to have the Images/jobId unique.
I wrote data code hundreds times.
Not sure what I’m missing here

Hi Dafna,
You have promises and async/await mixed in your code which can result in unexpected behavior like you’re experiencing.
I suggest to read https://support.wix.com/en/article/corvid-working-with-promises once again, choose a way you want to use and stick with it.

Thanks to Ohad I learned a lot today!!!

Attached is the code:

 
import wixData from 'wix-data';
import { fetch } from 'wix-fetch';

export async function updateJobs() {
 try {
 let results = await wixData.query("Jobs").find();
 let collectionlength = results.items.length;

 //empty Jobs collection
 for (let i = 0; i < collectionlength; i++) {
 let id = results.items[i]._id;
 let result1 = await wixData.remove("Jobs", id);
        }

 let jobNum;

 //import jobs
 let json = await fetchLever();
 let jsonLength = json.length;

 for (let i = 0; i < jsonLength; i++) {
 let listDescTitle, listDesc;
 let jobId, jobLang;

 if (json[i].lists[0] !== undefined) {
                listDescTitle = json[i].lists[0].text;
                listDesc = json[i].lists[0].content;

 let commitment = json[i].categories.commitment;
 if (commitment.includes("Time")) {
                    jobNum = listDesc.substr(7, 3);
                    jobLang = listDesc.substr(4, 3);
                } else {
                    jobNum = "";
                }
            }

 let listReqTitle, listReq;

 if (json[i].lists[1] !== undefined) {
                listReqTitle = json[i].lists[1].text;
                listReq = json[i].lists[1].content;
            }

 let list3Title, list3Desc;

 if (json[i].lists[2] !== undefined) {
                list3Title = json[i].lists[2].text;
                list3Desc = json[i].lists[2].content;
            }

 let text = json[i].text;

 let toInsertJobs = {
 "jobId": listDesc,
 "jobNum": jobNum,
 "text": text,
 "comitment": json[i].categories.commitment,
 "team": json[i].categories.team,
 "department": json[i].categories.department,
 "description": json[i].description,
 "decriptionPlain": json[i].descriptionPlain,
 "additionalPlain": json[i].additionalPlain,
 "hostedUrl": json[i].hostedUrl,
 "applyUrl": json[i].applyUrl,
 "location": json[i].categories.location,
 "listDescTitle": listDescTitle,
 "listDesc": listDesc,
 "listReqTitle": listReqTitle,
 "listReq": listReq,
 "list3Titele": list3Title,
 "list3Desc": list3Desc,
 "lang": jobLang
            };

 let jobsResults = await wixData.insert("Jobs", toInsertJobs)
 let item = jobsResults;

 if (item.jobNum !== "" && item.jobNum !== null && item.jobNum !== undefined && item.jobNum !== "000") {
 await addToImages(item.jobNum, item.text);
            }
        }
 await clearJobsFromImages();
    } catch (err) {
        console.log(err);
    }
}

export async function fetchLever() {
 let url = 'https://api.lever.co/v0/postings/abrahamhostels-2?mode=json';

 return await fetch(url)
        .then(response => response.json());
}

export async function addToImages(jobNum, text) {
    console.log("---enter addToImages. jobNum: " + jobNum);
 try {
 let results = await wixData.query("Images").eq("jobId", jobNum).find()

 let length = results.items.length;
        console.log("---imgsLength: " + length);

 if (length === 0) {
            console.log("---not found. jobNum: " + jobNum);

 let toInsertImages = {
 "jobId": jobNum,
 "title": text
            }
 try {
 let result = await wixData.insert("Images", toInsertImages);
            } catch (err) {
                console.log(err);
            }
        }
    } catch (err) {
        console.log(err);
    }
}

export async function clearJobsFromImages() {
 try {
 let ImagesResults = await wixData.query("Images").find();

 let imagesLength = ImagesResults.items.length;

 if (imagesLength > 0) {
 for (let i = 0; i < imagesLength; i++) {
 let imagesJobId = ImagesResults.items[i].jobId;

 try {
 let jobsResults = await wixData.query("Jobs").eq("jobNum", imagesJobId).find()

 let jobsLength = jobsResults.items.length;

 //remove if not exist
 if (jobsLength === 0) {
 try {
 let removeResult = await wixData.remove("Images", ImagesResults.items[i]._id)

                            console.log("...image removed jobNum = " + imagesJobId);

                        } catch (err) {
                            console.log(err);
                        }

                    }

                } catch (err) {
                    console.log(err);
                }
            }

        }
    } catch (err) {
        console.log(err);
    }
}

Your code looks very complicated (beyond my comprehension)
But why did you leave all the “catch” there?
also, instead of if (json[i].lists[0] !== undefined you should probably write

if(typeof json[i].lists[0] !== "undefined")

@jonatandor35 Hi, The ‘catch’ is for handling error cases.
undefined is when the field is undefined (length=0, but the length can not be measured, since the field is undefined).

“undefined” is a string with the value “undefinend” (length=9).

@dafna-kotzer I know what “catch” and “undefined” are. But I think you used them incorrectly.
You put a “catch” in an async function (even though it’s not a promise and I think you can’t do it). And when the variable is not assigned to any value, you need to use: if(typeof stringX === “undefined”) typeof checks for the type of the variable value, and it is “udefined” if it’s empty.
if it is a string with length zero, then you need if(stringX === “”)

If it’s undefined It’s not “”. You can’t ask for a value of undefined var

so use typeof