1
votes

I have a collection with following entry

{ 
    "_id" : "6z2pQDYozQxEyPZYv", 
    "userId" : "b2dQ6SuPrwmPsLsg8", 
    "communicatingWith" : [ "KMT74bWPoZxDSKdrx", "KMT74bWPoZxDSKdrx" ] 
}

when I query mongo through meteor for the field communicatingWith , if I do a console.log(communicatingWith), the output is [ 'KMT74bWPoZxDSKdrx', 'KMT74bWPoZxDSKdrx' ].

Even when I do console.log(communicatingWith.length) the output is 2

But when I do

communicatingWith.each(function(item){console.log(item)})

it throws error saying

Exception while invoking method 'createPrivateMsgHanger' TypeError: Object KMT74bWPoZxDSKdrx,KMT74bWPoZxDSKdrx has no method 'each'

Can you please help me understand where things are wrong?

3

3 Answers

1
votes

.each is a jQuery function pure javascript uses Array.forEach(function(item) { //do something here})

0
votes

I could get around this problem using the following statement let arr = Array.from(loggedInUserHanger.communicatingWith); once I have the arr, I can use all Array functions

0
votes

Another solution would be using forEach. I don't remember seing each in JS before https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach