I'm trying to use a for each loop to check if a user's ID is in a blacklist group I created. When I try to iterate over the string array of userID's, it says blacklisted.forEach is not a function
. Why is that?
query = { messageTrackingId: req.query.messageId };
messageId = true;
Messages.find(query)
.populate("creator", "username")
.then(documents => {
console.log("documents is");
console.log(documents[0].creatorId);
let otherUser;
if (documents[0].creatorId === req.query.creatorId) {
console.log("ITS A MATCH!")
otherUser = documents[0].recipientId;
}
else if (documents[0].recipientId === req.query.creatorId) {
console.log("ITS not a match!")
otherUser = documents[0].creatorId;
}
let blacklisted = false;
User.find({ _id: otherUser }).select("blacklistGroup").then((res) => {
blacklisted = res[0].blacklistGroup;
console.log("BLACKLIST SERVER RESPONSE");
console.log(blacklisted);
blacklisted.forEach(function(entry) {
console.log(entry);
});
CONSOLE OUTPUT
documents is
5e52cca7180a7605ac94648f
ITS not a match!
BLACKLIST SERVER RESPONSE
[ '5e52e8af484eba456ca9e814',
'5e52f2cc673de71f60019c76',
'5e52f316673de71f60019c77' ]
(node:12992) UnhandledPromiseRejectionWarning: TypeError: blacklisted.forEach is not a function