Reputation: 6790
I am having an issue with the following
{
artist:"Macy Gray"
song:"I Try'"
station:"PERTHRadio"
timeplay:2020-07-17T10:39:00.000+00:00
__v:0
history:Array
0:"7320564F-76B2-40D0-A0E8-E3917148F567"
1:"7320564F-76B2-40D0-A0E8-E3917148F567"
}
Basically it's adding the same UUID twice in history.
I am using a findOneAndUpdate
with $push
.
The code I am using
const nowplayingData = {
"station": req.params.stationname,
"song": data[1],
"artist": data[0],
"timeplay":npdate
};
LNowPlaying.findOneAndUpdate(
nowplayingData,
{ $push: { history: [uuid] } },
{ upsert: true },
function(err) {
if (err) {
console.log('ERROR when submitting round');
console.log(err);
}
}
);
Upvotes: 1
Views: 16
Reputation: 22276
Usually when people experience an issue like this It's because the function / route the code is in is being run twice. (Again -usually- this is due to debugging where the debugger is firing an extra call or something of the sort).
Regardless if this happens to you while debugging or in production you can just start using $addToSet instead of push, this will guarantee duplicate values will not be pushed.
LNowPlaying.findOneAndUpdate(
nowplayingData,
{ $addToSet: { history: [uuid] } },
{ upsert: true },
function(err) {
if (err) {
console.log('ERROR when submitting round');
console.log(err);
}
}
);
Upvotes: 2