2

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

10
  • 1
    You're sure it's an array? Try checking Array.isArray(blacklisted) and typeof blacklisted Commented Feb 23, 2020 at 22:02
  • It says Object. but the response looks like a string array. How can I convert it so that I can iterate over it? I don't think I can just JSON.stringify it since it would be more string than array. Commented Feb 23, 2020 at 22:05
  • Object is better than string, at least. Arrays are objects. What does Array.isArray show? Commented Feb 23, 2020 at 22:06
  • Array.isArray returns false Commented Feb 23, 2020 at 22:09
  • That's interesting. So it's an object (as arrays are), and is being logged with [ delimiters as if it's an array, but it's not. One could probably find a workaround by examining the object to find which properties hold the values you want (maybe try accessing [0], [1], etc), but it would be better to fix how the blacklistGroup object is being formed, since apparently it's not being formed as a plain array. Unfortunately, I don't know Mongoose Commented Feb 23, 2020 at 22:13

3 Answers 3

1

I'm not sure why it would display as an array if it's an object, but have you tried creating a new array from it and iterating over that? For example:

blacklisted = [...res[0].blacklistGroup];
blacklisted.forEach(function(entry) {
    console.log(entry);
});
Sign up to request clarification or add additional context in comments.

4 Comments

I added your code, but it outputs ``` UnhandledPromiseRejectionWarning: TypeError: res[0].blacklistGroup is not iterable```.
Not what I was expecting, but that makes some sense.
Do you think it would be efficient to stringify the data and then look for substrings matching userId? There could be times when that list could be over 100 users for some people, but who knows. Would there be performance concerns with doing that?
It certainly seems like there aught to be an easier way for you to access the data returned.
1

Is you "User" statement acting like a fetch? If so, you may have to convert your response to json before using it. Something like...

User.find({ _id: otherUser }).select("blacklistGroup")
    .then(res => res.json()) 
    .then(json => {
        blacklisted = json.blacklistGroup;
        console.log("BLACKLIST SERVER RESPONSE");
        console.log(blacklisted);
    });
});

1 Comment

That outputted TypeError: res.json is not a function Here's an example that will give you an idea of an unmodified output. Notice how it has _id in there too. pastebin.com/D0ZEB9AG
0

I realized that for some reason when I console.log(res[0].blacklistGroup) it was returning

[ '5e52e8af484eba456ca9e814',
  '5e52f2cc673de71f60019c76',
  '5e52f316673de71f60019c77' ]

which is also return type Object. So to solve this, I did the following:


let MyBlacklist = JSON.stringify(res[0].blacklistGroup);
MyBlacklist = JSON.parse(MyBlacklist);

then I was able to loop through

          let counter = 0;
          for (let i = 0; i < MyBlacklist.length; i++) {
            counter++;
            console.log(MyBlacklist[i]);
            console.log(counter);

          }

OUTPUT:

5e52e8af484eba456ca9e814
1
5e52f2cc673de71f60019c76
2
5e52f316673de71f60019c77
3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.