getItems() fuction

Hello,
I have some trouble from one week ago with getItems() fuction:
It’s look like the fonction have one lap of delay:
When I change the selection of my dropdown
the value of Item is the value of the selection N-1…
but the value of the dropdown if good…

I’m French and some comment are in french on the code

best regard

import wixWindow from 'wix-window';
import wixData from 'wix-data';
import wixUsers from 'wix-users';
//-------------Global Variables-------------//
// Options that have been selected.
let item={};
let txt_param = {1:'',2:'',3:'',4:'',5:''};
let param= {1:false,2:false,3:false,4:false,5:false};
//standard function
    function ReadItem(Dataset,index){
        $w(`#${Dataset}`).getItems(index,1)
            .then( (result) => {
                $w(`#${Dataset}`).onReady( () => {
                    item = result.items[0];
                });
            });
    }
//standard form
export function selectFormButton_change(event) {
    $w('#selectDesignButtonOp').enable();
    let test = $w('#selectFormButton').selectedIndex;
    console.error(test);
    ReadItem('dataset1',test);
    console.error(item);
    let txt_tmp= item.title  + ';' +
                 item.doigt1 + ';' +
                 item.doigt2 + ';' +
                 item.doigt3 + ';' +
                 item.doigt4 + ';' +
                 item.doigt5 + ';' +
                 item.doigt6 + ';' +
                 item.doigt7 + ';' +
                 item.doigt8 + ';' +
                 item.doigt9 + ';' +
                 item.doigt10;
    $w('#Parametre').text = ParamUpdate(1,txt_tmp,1);
}

Get rid of the lines in red:

  function ReadItem(Dataset,index){
        $w(`#${Dataset}`).getItems(index,1)
            .then( (result) => {
                $w(`#${Dataset}`).onReady( () => {
                    item = result.items[0];
               });
            });
    }

Hello,

thanks for your answer. Yes sorry, the onready() it’s mistask of me, I have add this for “try”.

my original function was this and it’s working one weeks ago (the function return nothing but if I print ‘tmp’ on the console before ‘return’ the value is ok):


    function ReadItem(Dataset,index){
        let tmp=[];
            $w(`#${Dataset}`).getItems(index,1)
            .then( (result) => {
                    tmp = result.items;
            });
        return tmp[0];
    }

I have modify my dropdown fuction like this , it’s not exactly what i expected but it’s work:

export function selectFormButton_change(event) {
    $w('#selectDesignButtonOp').enable();
            $w('#dataset1').getItems($w('#selectFormButton').selectedIndex,1)
            .then( (result) => {
                let item = result.items[0];
                let txt_tmp= item.title  + ';' +
                 item.doigt1 + ';' +
                 item.doigt2 + ';' +
                 item.doigt3 + ';' +
                 item.doigt4 + ';' +
                 item.doigt5 + ';' +
                 item.doigt6 + ';' +
                 item.doigt7 + ';' +
                 item.doigt8 + ';' +
                 item.doigt9 + ';' +
                 item.doigt10;
                $w('#Parametre').text = ParamUpdate(1,txt_tmp,1);
                ImgUpdate('ImgData1',item.type.image);
            });
    ValidPayButton(1,4);
}

best regard

In the onChange handler add this line and see what it logs and if it is correct.

console.log($w('#selectFormButton').selectedIndex);// should be 0 for the 1st option, 1 for the 2nd option etc...

Also make sure the order in the dropdown options is exactly like the order in your dataset.

Yes, All data was in correct place. but ok. the problem is when I try to extract the data outside of getItem function.
If I do Everything inside “getItems()”. it’s working, I will continue On this way…

thanks

I don’t see in your code any usage outside the getItems.then scope.