What does the NaN error mean?

I have been trying to formulate an output from the difference between two filters in different collections. I am getting the error NaN. This is my Js code.

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
user.getEmail()
.then( (email) => {
let userEmail = email;
} );
$w.onReady(() => {
Sum_amount();
});
let filter = wixData.filter().eq(“_owner”,userId)
export function Sum_amount(){
wixData.aggregate(“responses”)
.group(“_owner”)
.filter(filter)
.sum(“reward”,“sumHours1”)
.run()
wixData.aggregate(“custdetails”)
.group(“_owner”)
.filter(filter)
.sum(“withdraw”,“sumHours2”)
.run()
.then(
(results) => {
let diff=results.items[0].sumHours-results.items[0].sumHours1

$w(‘#text100’).text = diff.toString();
});

}

Anyone with an idea of how i can get #text100 as the difference between sumHours1 and sumHours2?

It Occus when you try to manipulate(add, sub, multiply…) a Number with not a number data type(String, Boolean) [NAN - “not an number”]
1 + undefined = 1- “a”= Nan

Try to Convert it to Number datatype and performe the operation again

//sample code
let sumHrs = Number(results.items[0].sumHours);
let sumHrs1 = Number(results.items[0].sumHours1)
let diff= sumHrs - sumHrs1;
console.log(sumHrs , sumHrs1, diff);// log the input for debuging

I noticed the Sum_amount function may have some error, I refactor the function


export async function Sum_amount(){
  const resP = wixData.aggregate("responses")
  .group("_owner")
  .filter(filter)
  .sum("reward","sumHours1")
  .run();
const custP = wixData.aggregate("custdetails")
  .group("_owner")
  .filter(filter)
  .sum("withdraw","sumHours2")
  .run()

  const [res , cust] = await Promise.all([resP, custP]);
  console.log({res, cust});
  const sumHrs = Number(res.items[0].sumHours1);
  const sumHrs1 = Number(cust.items[0].sumHours2);

  const diff = String(sumHrs - sumHrs1);
  console.log({sumHrs, sumHrs1, diff});

  $w('#text100').text = diff;
}

@salman-hammed There is a slight error on your code between sumHours1 and sumHours but It has worked perfect!!! THANK YOU!

Here is the correct code!
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
user.getEmail()
.then( (email) => {
let userEmail = email;
} );
$w.onReady(() => {
Sum_amount();
});
let filter = wixData.filter().eq(“_owner”,userId)
export async function Sum_amount(){
const resP = wixData.aggregate(“responses”)
.group(“_owner”)
.filter(filter)
.sum(“reward”,“sumHours1”)
.run();
const custP = wixData.aggregate(“custdetails”)
.group(“_owner”)
.filter(filter)
.sum(“withdraw”,“sumHours2”)
.run()
const [res , cust] = await Promise.all([resP, custP]);
console.log({res, cust});
const sumHrs = Number(res.items[0].sumHours1);
const sumHrs1 = Number(cust.items[0].sumHours2);
const diff = String(sumHrs - sumHrs1);
console.log({sumHrs, sumHrs1, diff});
$w(‘#text100’).text = diff;
}

I want to be a Corvid Master too some day! So help me God! @salman-hammed and @antonyb You guys are amazing!

@paiysurv Ah… yes Edited! I am not the one to add someone in Corvid Master but this is what i did? Help the community as much as you can and share the knowledge with others[Managing Forum, reporting Spam]
Soon you will be recognized by wix and their core team