I'm sending info to my database depending of the day and everything is working well but when I make the push to the array sending the new object, I'm creating an empty object and I don't want to create it.
This is my function that creates a new object:
const nuevoPick = async (req, res, e) => {
const tipsterId = req.body.tipster;
const title = req.body.title;
const description = req.body.description;
const fecha = req.body.fecha;
const category = req.body.category;
const pickImageUrl = req.body.pickImageUrl;
try {
const tipster = await Tipster.findById(tipsterId);
if (!tipster) {
return res.status(404).json({
msg: "Tipster no existe por ese ID",
});
}
let lastPickStored = tipster.picks.slice(-1)[0];
const yesterday = moment().subtract(1, "days").format("dddd, MMMM Do YYYY");
const date = moment().format("dddd, MMMM Do YYYY");
const ultimoPick = lastPickStored.data.slice(-1)[0].date;
const newPick = {
title,
description,
date,
category,
pickImageUrl,
};
if (ultimoPick === date) {
console.log("El último pick fue publicado hoy");
const tipsterUpdate = {
picks: [
...tipster.picks,
{
...lastPickStored.data.push(newPick),
},
],
};
console.log(tipsterUpdate);
const tipsterActualizado = await Tipster.findByIdAndUpdate(
tipsterId,
tipsterUpdate,
{
new: true,
}
);
res.json({
tipster: tipsterActualizado,
});
// Send Push Notification
const notification = await {
headings: {
en: " Nuevo Pick de " + tipster.name,
es: " Nuevo Pick de " + tipster.name,
},
contents: {
en: newPick.title,
es: newPick.title,
},
included_segments: ["Subscribed Users"],
android_channel_id: "4d196ac9-11b9-4f5b-beef-2b07e4f6c770",
};
const newPickNotification = await client.createNotification(notification);
} else {
console.log("El último pick fue publicao ayer");
const newPick = {
date,
data: [
{
title,
description,
date,
category,
pickImageUrl,
},
],
};
const tipsterUpdate = {
...req.body,
picks: [...tipster.picks, newPick],
};
const tipsterActualizado = await Tipster.findByIdAndUpdate(
tipsterId,
tipsterUpdate,
{
new: true,
}
);
res.json({
tipster: tipsterActualizado,
});
// Send Push Notification
const notification = await {
headings: {
en: " Nuevo Pick de " + tipster.name,
es: " Nuevo Pick de " + tipster.name,
},
contents: {
en: newPick.title,
es: newPick.title,
},
included_segments: ["Subscribed Users"],
android_channel_id: "4d196ac9-11b9-4f5b-beef-2b07e4f6c770",
};
const newPickNotification = await client.createNotification(notification);
}
} catch (error) {
console.log(error);
res.status(500).json({
ok: false,
msg: "Hable con el administrador",
});
if (e instanceof OneSignal.HTTPError) {
// When status code of HTTP response is not 2xx, HTTPError is thrown.
console.log(e.statusCode);
console.log(e.body);
}
}
};
But my response is this:
{
'0': {
date: 'Monday, February 21st 2022',
data: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
]
},
picks: [
{ date: 'Saturday, February 19th 2022', data: [Array] },
{ date: 'Sunday, February 20th 2022', data: [Array] },
{ date: 'Monday, February 21st 2022', data: [Array] },
{}
]
}
With that last empty object on the picks array that I don't want to create, can someone help me to find why I'm creating that empty array?
Thank you, I appreciate it.