I have this code which already works :
getAllNew: async (req, res) => { //This code is what gives us only events that are going to happen, and discards the ones in the past.
try {
const eventsWithLowestPrice = []
const eventosconboletos = []
const events = await User.getF()
for (const event of events) {
const date1 = stringToDate(event.fecha_fin);
const date2 = stringToDate(today);
if (date1 >= date2 && event.hora_fin > my_time) {
eventsWithLowestPrice.push(event.id_evento_fk)
}
}
for (const eventWithLowestPrice of eventsWithLowestPrice) {
const publication = await Event.getOne(eventWithLowestPrice);
eventosconboletos.push(publication)
}
res.json(eventosconboletos)
} catch (error) {
console.log(error)
res.status(500).json({ msg: 'Error al consultar eventos' })
}
},
The problem is that the events that I'm getting are duplicates in the final push.
Everything is working, but i don't know first which array is the one holding the duplicates and how to get rid of them.
CodePudding user response:
i think you should check eventsWithLowestPrice if id exist on the array do not push it:
getAllNew: async (req, res) => { //This code is what gives us only events that are going to happen, and discards the ones in the past.
try {
const eventsWithLowestPrice = []
const eventosconboletos = []
const events = await User.getF()
for (const event of events) {
const date1 = stringToDate(event.fecha_fin);
const date2 = stringToDate(today);
if (date1 >= date2 && event.hora_fin > my_time) {
if(eventsWithLowestPrice.findIndex(i => i === event.id_evento_fk) === -1)
eventsWithLowestPrice.push(event.id_evento_fk)
}
}
for (const eventWithLowestPrice of eventsWithLowestPrice) {
const publication = await Event.getOne(eventWithLowestPrice);
eventosconboletos.push(publication)
}
res.json(eventosconboletos)
} catch (error) {
console.log(error)
res.status(500).json({ msg: 'Error al consultar eventos' })
}
},
this code if(eventsWithLowestPrice.findIndex(i => i === event.id_evento_fk) === -1)
check if event id does not exist on eventsWithLowestPrice list so this is okey push it to array
I hope I understand your question currently