TypeError: $w(...).getCurrentItem is not a function

I’m getting this Error and the code after this error won’t work

import wixUsers from 'wix-users';
import wixData from 'wix-data';

let currentItem = $w("#myDataset").getCurrentItem();
console.log(currentItem);

I’m a bit confused because the API reference. Is it a function? or something else?

The only way that makes it work for me is to put it like this both onReady is a must:

$w.onReady( () => {
  $w("#myDataset").onReady( () => {
    let currentItem = $w("#myDataset").getCurrentItem();
    console.log(currentItem);
  } );
} );

However, in this way I cannot return the currentItem and use it in other line of the code.

For example, if I want to use a chart to show numbers related to currentItem or if I want to export function button_onClick() working with currentItem. How can I do that?

Is there another way to go around or fix this issue “TypeError: $w(…).getCurrentItem is not a function”?

Thx

Taht is obvious that you get this ERROR, because it is really not coded as a function, but what if you do something like that…?

$w.onReady( () => {
  $w("#myDataset").onReady( () => {
    xxx();
  } );
} );
function xxx() {
     let currentItem = $w("#myDataset").getCurrentItem();
    console.log(currentItem);
}

You can create your functions, which you want, just call them right after the dataset is READY!

There are 2-types how to code!

example-1

$w.onReady(function () {   });
export function button1_mouseIn(event) {$w("#BannerStateBox").changeState("BestSeller");}
export function button2_mouseIn(event) {$w("#BannerStateBox").changeState("OutOfStock");}
export function button3_mouseIn(event) {$w("#BannerStateBox").changeState("New");}

example-2

$w.onReady(function () {   
    $w('#button1').onClick(() => {$w("#BannerStateBox").changeState("BestSeller");})
    $w('#button2').onClick(() => {$w("#BannerStateBox").changeState("OutOfStock");})
    $w('#button3').onClick(() => {$w("#BannerStateBox").changeState("New");})           
});

The live example can be seen here…
https://russian-dima.wixsite.com/meinewebsite/multistate-boxes

In your case it is recomended to use this technique…

$w.onReady( () => {
  $w("#myDataset").onReady( () => {
    $w('#YOUR_BUTTON').onClick(() => {
      DO SOMETHING
    })
  });
});

Thanks.:blush:
You got it right and it works.:white_check_mark:
This is my code example below.

function xxx() {
    let currentItem = $w("#dataset").getCurrentItem();
	let currentItemId = currentItem._id
	console.log(currentItemId);
	return currentItemId;
}

$w.onReady( () => {
  $w("#dataset").onReady( () => {
    xxx();
	$w('#text').text = xxx();
  } );
} );

@abdullahalnabi
No problem! You are welcome!:sweat_smile: