N
Nuxt7mo ago
Niza

Typescript Function & Method calls bring 500 error

Hi, I've noticed basic typescript methods don't work in nuxt. For example
appointments.filter(appointment => appointment.date.getTime() > currentTime.value.getTime());
appointments.filter(appointment => appointment.date.getTime() > currentTime.value.getTime());
I get the following error:
[nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'filter')
[nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'filter')
This is just one of the many methods and function calls (such as JSON.parse etc). Could anyone help me understand what I'm doing wrong?
6 Replies
Niza
NizaOP7mo ago
No description
Niza
NizaOP7mo ago
My code has types
No description
Niza
NizaOP7mo ago
oh Let me check
Niza
NizaOP7mo ago
You're right. My bad
No description
Niza
NizaOP7mo ago
No description
Niza
NizaOP7mo ago
import { AppointmentType } from "~/types/Appointment";

export default defineEventHandler(async (event) => {
const query = getQuery(event)

if (query.userid) {
try {
let data: any = await appointmentsModel.find({ userid: query.userid });
if (query.latestAppointmentsOnly == "true") {
let appointments: AppointmentType[] = data as unknown as AppointmentType[];
data = appointments.filter(appointment => appointment.date.getTime() > (new Date()).getTime());
return { data };
}
return { data }
}
catch (error) {
return error
}
}
})
import { AppointmentType } from "~/types/Appointment";

export default defineEventHandler(async (event) => {
const query = getQuery(event)

if (query.userid) {
try {
let data: any = await appointmentsModel.find({ userid: query.userid });
if (query.latestAppointmentsOnly == "true") {
let appointments: AppointmentType[] = data as unknown as AppointmentType[];
data = appointments.filter(appointment => appointment.date.getTime() > (new Date()).getTime());
return { data };
}
return { data }
}
catch (error) {
return error
}
}
})
Solved it by processing it on an api endpoint Right from the source of it P.S I am using Nuxt-Mongoose
Want results from more Discord servers?
Add your server