1

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?

chridam
  • 100,957
  • 23
  • 236
  • 235
boredmgr
  • 272
  • 1
  • 7
  • 20

3 Answers3

1

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

MrE
  • 19,584
  • 12
  • 87
  • 105
0

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

boredmgr
  • 272
  • 1
  • 7
  • 20
0

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

Radu Chiriac
  • 1,374
  • 3
  • 18
  • 34