BeforeInsert adds Item to collection although conditions are not fulfilled

Hello friends,

I would like to use BeforeInsert + wixData.query() to check if a customer has already been added to my collection (CMS).

The problem is regardless of whether the condition is fulfilled or not the customer is always created.

Info:
I have created several input fields on my page.
First name, last name, email, phone number, address and date of birth.
With a button the entries of the fields are linked to my collection “Link to CMS”.

What I also noticed, even if I comment out the “return item”. The item is still added to my collection.

data.js:

import wixData from 'wix-data';

export function Kundendaten_beforeInsert(item, context) {
    var vorname = item.vorname.toLowerCase();
    var nachname = item.title.toLowerCase();
    var email = item.eMail.toLowerCase();

    var doppelterUser = false;

    wixData.query("Kundendaten")
        .eq("vorname", vorname)
        .eq("title", nachname)
        .eq("eMail", email)
        .find()
        .then(results => {
            if (results.items.length > 0) {
                doppelterUser = true;
            }
        })
    if (doppelterUser) {
        console.log("Error User exists")
        return false;
    } else {
        return item;
    }
}

to test if beforeInsert Function triggers:
with the second code, the item is stored in my collection. The item is stored as “debugName” and so on

import wixData from 'wix-data';

export function Kundendaten_beforeInsert(item, context) {
    //var vorname = item.vorname.toLowerCase();
    //var nachname = item.title.toLowerCase();
    //var email = item.eMail.toLowerCase();

    var vorname = "debugName";
    var nachname = "debugName2";
    var email = "debugmail@outlook.de";
    var tel = 123;
    var geburtsdatum = "1991-02-03";
    var addr = "Yakima, Washington, USA";


   const changedItem = {
                    vorname: vorname,
                    title: nachname,
                    eMail: email,
                    address: addr,
                    telefonnummer: tel,
                    date: geburtsdatum,
                }

	item = { ...item, ...changedItem }
    return item;

the last code does not work again. The item will not be adjusted, but the original entries from the input fields will be passed.

Can someone also tell me what it is about the WIX editor that displays the “return” blue, usually it is red?

import wixData from 'wix-data';

export function Kundendaten_beforeInsert(item, context) {
    //var vorname = item.vorname.toLowerCase();
    //var nachname = item.title.toLowerCase();
    //var email = item.eMail.toLowerCase();

    var vorname = "debugName";
    var nachname = "debugName2";
    var email = "debugmail@outlook.de";
    var tel = 123;
    var geburtsdatum = "1991-02-03";
    var addr = "Yakima, Washington, USA";

    await wixData.query("Kundendaten")
        .eq("vorname", vorname)
        .eq("title", nachname)
        .eq("eMail", email)
        .find()
        .then(results => {
            if (results.items.length > 0) {
                return Promise.reject("User exists");
            } else {
                const changedItem = {
                    vorname: vorname,
                    title: nachname,
                    eMail: email,
                    address: addr,
                    telefonnummer: tel,
                    date: geburtsdatum,
                }
                item = { ...item, ...changedItem }
                return item; //Why wix Editor shows "return" in Color Blue? and not normally red
            }
        })
        .catch(error => {
            return Promise.reject(error);
        });
}

Hope u can help me :slight_smile:

1 Like

Please help me with my issue.
I really need your help guys.