Waiting for "Returned"

This is the code:

$w(‘#dataset1’).new()
$w(‘#dataset1’).setFieldValue(“codFirst”, “School”)
$w(‘#dataset1’).setFieldValue(“codSecond”, “OriginalSchool”)
$w(‘#dataset1’).save()

But this happening

[wix-dataset.new] called
[wix-dataset.setFieldValue] called with ( ) // try to setfieldvalue
DatasetError: There is no current item // try to save
[wix-dataset.new] returned // but just now the “called” returned

So this all get wrong!! Someone can help me?

Hi @samuel-milanez

You don’t need the “new” command at the beginning, you should use the setField command inside the dataset onBeforeSave event handler, you also need the dataset to be in Read / Write mode, but if you want to add new entry to the database, don’t use setField and save , use the wixData insert command instead.

async function addToDatabase() {
    let newtItem = {
        field1: "Field 1 Value",
        field2: "Field 2 Value,
        field3: name
    };

    let result = await wixData.insert("CollectionName", newItem);
}

This is a better way to save new entries to the database automatically without a button that the user need to click on in order to save the data, if you want a seamless experience, the second way is the good way to go.

Happy Coding :wink:.
AHmad.

Thanks AHmad for your answere, I think we close to finish this.

In the end of code the newItem is getting a error (‘newItem is not defined’)

This is the all code:


export function dataset3_ready() {
    if ($w('#dataset3').getCurrentItem("allName") === null) {
        async function addToDatabase() {
            let newtItem = {
                codAsk: "ElvaBarberSchool",
                codYes: "ElvaBarberSchool",
                allName: $w('#dataset2').getCurrentItem().name
            };
             let result = await wixData.insert("Profile", newItem);
        }
    }
}

dataset3 have the name “Profile”, and this are “read/write” with filter “Owner is Logged-in user”
codAsk, codYes, and allName are the 3 fields I want to write.

The onReady( ) event handler is not written correctly, and you’re giving callbacks on the getCurrentItem while it doesn’t accept any, also, you can’t, or shouldn’t compare it to null, dynamic pages always have current items, otherwise, you’ll get a (404) error when visiting the page, so it doesn’t make any sense.

Furthermore, you’re missing the closing ( } ) on the let item declaration, that’s probably why you’re getting an undefined error.

I didn’t understand what you’re trying to achieve or do, please explain and provide additional details in order for me to be able to assist you properly.

Ok, lets try, LOL

I have a database: “profile”.
Each member of the site must have only one registration there.
This registration must be done automatically (without the need for a member click).
In it I need the student’s name, and in two fields the name “ElvaBarberSchool”.

As there can be no duplicates, the code needs to read first if there are any records and only launch if one of the fields is null.

I dont know if this is the right way, but it worked:




export function dataset2_ready() {
    if ($w('#dataset3').getCurrentItem("allName") === null) {
        let toInsert1 = {
            "codAsk": "EscolaElvaBarber",
            "nomeCompleto": $w('#dataset2').getCurrentItem().name,
            "codYes": "EscolaElvaBarber"
        };
        wixData.bulkInsert("Perfil", [toInsert1]);
        $w('#dataset3').save()
    }
}