简体   繁体   中英

foreach loop not iterating over string array

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

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);
});

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);
    });
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM