# Solved - Error: Cannot read properties of undefined

**URL:** <https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [March 7, 2023, 9:52am UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886 "2023-03-07T09:52:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![earthushare](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/earthushare/32/36710_2.png) [@earthushare](https://forum.wixstudio.com/u/earthushare)\
**Post date:** [March 7, 2023, 9:52am UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/1 "2023-03-07T09:52:01Z")

</div>

Hello here,

I use this code :

```javascript
export function iParrain_input(event) {
  $w("#dataset1").setFilter(wixData.filter()
    .eq("idClient",$w("#iParrain").value))
    .then(result);
}

function result() {
  let total = Number($w('#dataset1').getTotalCount());

  if (total === 1 ){
    $w('#bInscription').enable();
    $w('#errorReco').hide();
  }
  
  if (total !== 1 ){
    $w('#bInscription').disable();
    $w('#errorReco').show();
  }
}

```

and i had on the logs :

```javascript
Error: Cannot read properties of undefined (reading 'total')

```

But all works, so is it a problem or not ?

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [March 7, 2023, 5:45pm UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/2 "2023-03-07T17:45:07Z")

</div>

This means that sometimes in your code, your variable “total” is undefined (versus containing a value or number). I am not exactly sure from looking at your provided code where this is happening, but you can debug to find out and then write code for what should happen if total is undefined.

As a general practice, most would say you should address errors but can pass over on warnings (in most cases) so I would personally see if you can get ot the bottom of it

---

<div class="post-metadata">

**Author:** ![earthushare](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/earthushare/32/36710_2.png) [@earthushare](https://forum.wixstudio.com/u/earthushare)\
**Post date:** [March 7, 2023, 6:29pm UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/3 "2023-03-07T18:29:27Z")

</div>

Ok thanks so i’ve just to put this variable in a global variable i’ll try it thx

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [March 7, 2023, 6:48pm UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/4 "2023-03-07T18:48:26Z")

</div>

Your dataset is ready at the point when you start your filterings on it ?  
Maybe you want to read this…

> **[\[SOLVED\] \[SOLVED\] Save information using default settings and code | Velo by Wix](https://community.wix.com/velo/forum/coding-with-velo/save-information-using-default-settings-and-code)**
>
> Hi everyone, hoping you're doing well! 😁
> 
> Here's the case: I use the default settings to save info to a database (this data will be uploaded by a user). So then, I have a folio number that is calculated automatically, using code. I want that folio...

Maybe you also want to change your coding-syntax in future, who knows.

---

<div class="post-metadata">

**Author:** ![earthushare](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/earthushare/32/36710_2.png) [@earthushare](https://forum.wixstudio.com/u/earthushare)\
**Post date:** [March 7, 2023, 9:33pm UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/5 "2023-03-07T21:33:54Z")

</div>

i just need to know if the number i put inside already exist in the database, but i go look ur link

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [March 7, 2023, 10:01pm UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/6 "2023-03-07T22:01:46Z")

</div>

1. You don’t have to define it doubled…

```javascript
if (total === 1 ){
    $w('#bInscription').enable();
    $w('#errorReco').hide();
}

if (total !== 1 ){
    $w('#bInscription').disable();
    $w('#errorReco').show();
}

```

Correct way…

```javascript
if (total !== 1 ){
    $w('#bInscription').disable();
    $w('#errorReco').show();
}
else {
    $w('#bInscription').enable();
    $w('#errorReco').hide();
}

```

1. Working with DATASETS → you always need to wait for your dataset first, until it is ready…

```javascript
$w.onReady(()=>{console.log("Page is ready...);
    $w('#dataset1').onReady(()=>{
    	    console.log('Dataset ready);
	    $w('#iParrain_input').onChange(()=>{
		  console.log("INPUT-CHANGED!!!!!");
		  //all the rest of your code here....
		  //all the rest of your code here....
		  //all the rest of your code here....
		  //all the rest of your code here....
		  //all the rest of your code here....
		  //all the rest of your code here....
		  //all the rest of your code here....
		  //all the rest of your code here....
		  //starting a function.....
		    myFunction();	
	    });	
    });
});

```

1. I am almost sure, that i already saw this INCORRECT-CODE somewhere.  
Didn’t i already replied? Do you have a second opened POST ???

2. Generating your wished function …

```javascript
function myFunction() {
	//CODE of FUNCTION...
	//CODE of FUNCTION...
	//CODE of FUNCTION...
	//CODE of FUNCTION...
	//CODE of FUNCTION...
}

```

Work more logicaly…

After you have read the provided POST, you also will be able to work with RETURNING-FUNCTIONS.

You also will be able to recognize the new presented CODE-SYNTAX.

You also will be able to USE-Wix-DATA to get your results from dATABSE.

You also will be able to recognise when to use ASYNC-AWAIT!

BTW… WHAT IS ASYNC-AWAIT ? CAN I EAT IT ?

First try to learn JS-BASICS, before you try to go deeper into coding.

And when you have done all your homework…your result should look like this one, later…

```javascript
$w.onReady(()=>{console.log('Page is ready...');
    $w('#dataset1').onReady(()=>{
            console.log('Dataset ready...');
        $w('#iParrain_input').onChange(()=>{
          console.log("INPUT-CHANGED!!!!!");
          let inpValue = $w('#iParrain_input').value;
          console.log("My-Value: ", inpValue);
          //all the rest of your code here....
          //all the rest of your code here....
          //all the rest of your code here....
          //all the rest of your code here....
          //all the rest of your code here....
          //all the rest of your code here....
          //all the rest of your code here....
          //all the rest of your code here....
          //starting a function.....
            myFunction(inpValue);   
        }); 
    });
});

function myFunction(inpValue) {
    let filter = wixData.filter();
    filter = filter.eq("idClient", inpValue);
    $w("#dataset1").setFilter(filter)
    .then((res)=>{console.log(res);
        let total = Number($w('#dataset1').getTotalCount());
        console.log(total);
        if (total !== 1 ){
            $w('#bInscription').disable();
            $w('#errorReco').show();
        }
        else {
            $w('#bInscription').enable();
            $w('#errorReco').hide();
        }
    }).catch((err)=>{console.log(err);})
}

```

In this case —\> using your dataset to set the filter.

Pay attention onto the ORANGE-MARKED CODE-PART.  
Take a look into CONSOLE, what do you het ?

2-different scenarios are possible…  
a) you get some results → inspect them and use them.  
b) you get nothing → in this case → use the → catch().-method to catch the ERROR.

---

<div class="post-metadata">

**Author:** ![earthushare](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/earthushare/32/36710_2.png) [@earthushare](https://forum.wixstudio.com/u/earthushare)\
**Post date:** [March 7, 2023, 11:56pm UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/7 "2023-03-07T23:56:04Z")

</div>

Thanks, I’ll take it all back.

1. modified
2. I didn’t know, I’ll modify it accordingly
3. I have indeed a second post open but not on the same subject exactly. The code is taken on internet I don’t have enough knowledge to realize that 😕
4. I have only one application to realize in velo/js. I’m trying to make a functional code for the site. I don’t intend to become a professional developer but I’m learning little by little. I started velo/js 3 days ago so please be accommodating 🙂

---

<div class="post-metadata">

**Author:** ![earthushare](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/earthushare/32/36710_2.png) [@earthushare](https://forum.wixstudio.com/u/earthushare)\
**Post date:** [March 8, 2023, 12:02am UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/8 "2023-03-08T00:02:25Z")

</div>

I also found out where the error came from.  
I used to use :

```javascript
export function iParrain_input(event) {

```

When typing too fast the code didn’t have time to execute and created errors

so I replaced by :

```javascript
export function iParrain_change(event) {

```

And it works now

---

<div class="post-metadata">

**Author:** ![earthushare](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/earthushare/32/36710_2.png) [@earthushare](https://forum.wixstudio.com/u/earthushare)\
**Post date:** [March 8, 2023, 12:41am UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/9 "2023-03-08T00:41:31Z")

</div>

I’ve done that for now it’s functional and I’ll move on the rest slowly, thanks 🙂

```javascript
// ********************************Import of libraries********************************

import { authentication } from 'wix-members';
import wixData from 'wix-data';
import {memory} from 'wix-storage';

// ********************************Declaration of global variables********************************

// ********************************Functions********************************

// ---------- Checking the validity of the sponsor code ----------

function f_verifParrain(code) {

  // ===== Set up Filter ====
  let filter = wixData.filter();
  filter = filter.eq("idClient", code);

  // ===== Search in the database ====
  $w("#dataset1").setFilter(filter)
  .then(()=>{

    // ===== Retrieve the number of sponsors ====
    let total = Number($w('#dataset1').getTotalCount());
    console.log("Number of occurrences : " + total);

    // ===== Invalid code ====
    if (total !== 1 ){
        $w('#bInscription').disable();
        $w('#errorReco').show();
    }
    // ===== Valid code ====
    else {
        $w('#bInscription').enable();
        $w('#errorReco').hide();
    }
  }).catch((err)=>{console.log(err);})
}

// ********************************Code start********************************

// ---------- Wait for the page to load ----------

$w.onReady(()=>{console.log('Page is ready');

// ---------- Waiting for the Database to load --------

  $w('#dataset1').onReady(()=>{
    console.log('Dataset1 ready');

    // ---------- Detection of the filling of the recommendation field ----------

    $w('#iParrain').onChange(()=>{
      console.log("Code Changed");
      let codeSponsor = $w("#iSponsor").value;
      console.log("code input " + codeSponsor)
      f_verifSponsor(codeSponsor);
    });
  });
});

```

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [March 8, 2023, 6:49am UTC](https://forum.wixstudio.com/t/solved-error-cannot-read-properties-of-undefined/3886/10 "2023-03-08T06:49:07Z")

</div>

If it is working for you, then everything is good.
