Solved - Error: Cannot read properties of undefined

Hello here,

I use this code :

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 :

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

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

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

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

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

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

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

  1. You don’t have to define it doubled…
if (total === 1 ){
    $w('#bInscription').enable();
    $w('#errorReco').hide();
}

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

Correct way…

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…
$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 …

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…

$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.

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 :confused:
  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 :slight_smile:

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

export function iParrain_input(event) {

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

so I replaced by :

export function iParrain_change(event) {

And it works now

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

// ******************************** 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);
    });
  });
});

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