I'm trying to remove a value from an array in my database which I get from url.
I use the code below but I get nothing. It doesn't go to the for loop.
app.get('/remove/:medid/:tokenid', function(req, res) {
var medid = req.params.medid;
var token = req.params.tokenid;
var query = { tokenid: token, mediaid !== 'undefined' && mediaid.length > 0 }
user.find(query).exec(function(err, result) {
if (err) {
res.send('erooooooor')
}
if (!result) {
res.send('whooops, you dont have any media yet :)')
} else {
console.log('its here')
for (var i in result.mediaid) {
console.log(i)
if (i == medid) {
user.update({ tokenid: token }, { $pull: { mediaid: medid } }, function(err, result2) {
if (err) {
res.send('an error happened')
} else {
console.log('deleted')
//res.send('your tokenid is '+ token)
}
})
} else {
res.send('media id didnt match')
}
}
}
});
});
My database has 3 objects userid and tokenid which are strings and mediaid which is an array.
Also, I want to check if my mediaid array is null or not and exist is this code
mediaid !== 'undefined' && mediaid.length > 0