There must be an easier way to filter a new field created using data.js, add it into the collection and display it in a table: using code?

I have a table showing appointments for my patients.

If appointments have passed (ie. less than today’s date) they should not be shown in the table and therefore not be valid.

I use this code to define that.

data.js
if (appDate > yesterday) {
item.validApp = “Yes” ;
}
if (appDate < today) {
item.validApp = “No” ;
}
return item;
}

This code creates 3 valid appointments in the live collection.

The table only shows 2 valid appointments.

I’m stuck.

It’s not clear what you’re trying to do and which hook you’re using.

Want to see only appointments which are = or > than today, shown in my appointments table.

I use data.js to define the field and then want to show the newly created value as the filter parameter.

@digitalzemb Post your full code. I don’t see the hook function, I don’t see where you declare the date variables.

@jonatandor35

import wixData from ‘wix-data’ ;

export function Consultations_afterQuery(item, context) {
item.ConsultID = item._id.slice( 0 , 8 ).toUpperCase();
item.consultCode = item.patientID + “-” + item.ConsultID;
const eyes = item.gcsEyes;
const verbal = item.gcsVerbal;
const motor = item.gcsMotor;
item.gcs = Number(eyes) + Number(verbal) + Number(motor);

return item;

}

// …

export function Contacts_afterQuery(item, context) {
//item.Geolocation = item.latitude +“,”+ item.longitude;
//firstName==================================
const fstr = item.firstName;
const lstr = item.lastName;
function capitalize(string) {
return string.charAt( 0 ).toUpperCase() + string.slice( 1 );
}

const capsfname = fstr.split( ’ ’ ).map(capitalize).join( ’ ’ );
const capslname = lstr.split( ’ ’ ).map(capitalize).join( ’ ’ );

item.firstName = capsfname;
item.lastName = capslname;
item.fileName = capslname + “, " + capsfname + " (” + item.title + “)” ;
item.PatientID = item._id.slice( 0 , 4 ).toUpperCase();
item.fileNo = item.lastName + " [" + item.PatientID+ “]” ;
item.PtID = item._id;

//==========================================

var appTime = item.appTime;
var appDate = item.appDate- 1 ;
var DOB = item.DOB;

const days = [
‘Domingo’ ,
‘Lunes’ ,
‘Martes’ ,
‘Miércoles’ ,
‘Jueves’ ,
‘Viernes’ ,
‘Sábado’
]

const months = {
0 : ‘Enero’ ,
1 : ‘Febrero’ ,
2 : ‘Marzo’ ,
3 : ‘Abril’ ,
4 : ‘Mayo’ ,
5 : ‘Junio’ ,
6 : ‘Julio’ ,
7 : ‘Agosto’ ,
8 : ‘Septiembre’ ,
9 : ‘Octubre’ ,
10 : ‘Noviembre’ ,
11 : ‘Diciembre’
}

const n = (DOB) //Math.round(DOB);
const d = new Date(appDate);
//const y = Math.round(((((Date.now() - new Date(n))/1000)/60)/60)/24/365).toFixed(1);
const y = (((((Date.now() - new Date(n))/1000)/ 60 )/60)/ 24 / 365 ).toFixed( 1 );
const yyyy = d.getFullYear(appDate);
const dayName = days[d.getDay(appDate)]
const dd = d.getDate(appDate);
const monthIndex = d.getMonth(appDate);
const monthName = months[monthIndex];
const today = Date.now();
const yesterday = Date.now()- 1 ;
item.years = y;
item.dayName = dayName;

item.appSlot = dd + " " + monthName + " " + yyyy + " @ " + appTime

if (appDate > yesterday) {
item.validApp = “Yes” ;
}
if (appDate < today) {
item.validApp = “No” ;
}
return item;
}

@digitalzemb
Date.now() - 1 ; means minus 1 millisecond.

@jonatandor35 Ok - changed to
const yesterday = new Date( new Date().setDate( new Date().getDate()- 1 ));
Filter still shows 2 out of the 3 “ValidApp” appointments as valid. This change has no bearing on the validity of the date at all.

J.D. - whilst I really appreciate your help, using Javascript is impossibly frustrating for me. I get some things right and others just don’t want to work. I don’t have time to study Javascript and learn how to do basic stuff just so I can see who my next appointments are with. Hence my Question above.

I don’t have time to review your full code and locate the issues, but basically if you have with a number that represents dates and you want to set an afterQuery value based on it, then you you should put in the afterQuery hook:

const now = new Date();
now.setHours(0);
now.setMilliseconds(0);
now.setMinutes(0);
now.setMonth(0);
now.setSeconds(0);
now = now.getTime();
item.dateField > now ? item.isValid = true : item.isValid = false;
return item;

In this example it set a true value for today and the future and false value for yesterday and before that.