1

我有一个包含以下条目的集合

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

当我通过流星查询mongo的字段communicationWith时,如果我做一个console.log(communicatingWith),输出是 [ 'KMT74bWPoZxDSKdrx', 'KMT74bWPoZxDSKdrx' ]

即使我这样做console.log(communicatingWith.length),输出也是 2

但是当我这样做时

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

它抛出错误说

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

你能帮我理解哪里出了问题吗?

4

3 回答 3

1

.each是纯 javascript 使用的 jQuery 函数Array.forEach(function(item) { //do something here})

于 2016-01-05T00:45:38.540 回答
0

另一种解决方案是使用forEach. 我不记得在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEacheach之前看过 JS

于 2016-01-03T17:58:36.810 回答
0

我可以使用以下语句来解决这个问题 let arr = Array.from(loggedInUserHanger.communicatingWith); 一旦我有了 arr,我就可以使用所有 Array 函数

于 2016-01-03T15:57:00.043 回答