如何定义数组?

时间:2019-01-26 12:35:19

标签: javascript node.js mongodb

我正在遍历数据库查询

(async () =>{
    let client;
    try {
        client = await MongoClient;
        let collection = client.db("booliscraper").collection("sold");
        let docs = await collection.find();
        await docs.forEach((sale) => {
            console.log(sale.location.namedAreas, typeof sale.location.namedAreas);
            sale.street = sale.location.namedAreas[0]
        });
    client.close(); 

  } catch(err) {
    log(err)
  }
})()

我得到这样的输出,其中有100个:

[ 'Kungsholmen' ] 'object'
[ 'Blåsut' ] 'object'
[ 'Östermalm' ] 'object'
[ 'Beckomberga Bromma' ] 'object'
[ 'Södermalm' ] 'object'
[ 'Johanneshov' ] 'object'
[ 'Vasastan' ] 'object'
[ 'Hässelby-Vällingby' ] 'object'
[ 'Vasastan' ] 'object'
[ 'Östermalm' ] 'object'
[ 'Norra Djurgårdsstaden' ] 'object'
[ 'Gröndal' ] 'object'
[ 'Norrmalm' ] 'object'
[ 'Minneberg' ] 'object'
[ 'Bromma Brommaplan' ] 'object'
[ 'Älvsjö' ] 'object'
[ 'Vasastan' ] 'object'

突然,在最后一个之后,它抛出一个错误:

/Users/kristoffernolgren/web/booliscrape/node_modules/mongodb-core/lib/cursor.js:206       犯错       ^

TypeError: Cannot read property '0' of undefined
    at docs.forEach (/Users/kristoffernolgren/web/booliscrape/updaterecords.js:15:42)

就是这一行:

            sale.street = sale.location.namedAreas[0]

为什么['Vasatstan']没有属性[0]

docs中包含40000个项目。但是,这是一个示例:

{ _id: 5beca43b78f21248ab47f835,
  location:
   { address: { streetAddress: 'Sankt Eriksgatan 22' },
     position: { latitude: 59.3315552, longitude: 18.03116681 },
     namedAreas: [ 'Kungsholmen', [length]: 1 ],
     region:
      { municipalityName: 'Stockholm', countyName: 'Stockholms län' },
     distance: { ocean: 2252 } },
  listPrice: 5395000,
  rent: 1517,
  floor: 2,
  livingArea: 73,
  additionalArea: 0,
  source:
   { name: 'Bjurfors',
     id: 1561,
     type: 'Broker',
     url: 'http://www.bjurfors.se/' },
  rooms: 3,
  published: 2018-08-09T12:21:49.000Z,
  constructionYear: 1984,
  objectType: 'Lägenhet',
  booliId: 3195821,
  soldDate: 2018-10-27T00:00:00.000Z,
  soldPrice: 5430000,
  apartmentNumber: '1204',
  soldPriceSource: 'bid',
  publishedDays: 708,
  soldDays: 786,
  daysUp: 78,
  street: 'Kungsholmen',
  streetYear: 'Kungsholmen 1984',
  yearDay: 'something' 
}

这是使它破裂的一次:

{ _id: 5beca4cf78f21248ab48087b,
  location:
   { address: { streetAddress: 'Sankt Eriksgatan 71' },
     position: { latitude: 59.3385408, longitude: 18.03577423 },
     namedAreas: [ 'Vasastan', [length]: 1 ],
     region:
      { municipalityName: 'Stockholm', countyName: 'Stockholms län' },
     distance: { ocean: 2230 } },
  listPrice: 4795000,
  rent: 2462,
  floor: 4,
  livingArea: 54,
  additionalArea: 0,
  source:
   { name: 'Bjurfors',
     id: 1561,
     type: 'Broker',
     url: 'http://www.bjurfors.se/' },
  rooms: 2,
  published: 2018-08-10T10:15:06.000Z,
  constructionYear: 1929,
  objectType: 'Lägenhet',
  booliId: 3188830,
  soldDate: 2018-08-23T00:00:00.000Z,
  soldPrice: 5270000,
  soldPriceSource: 'bid',
  url: 'https://www.booli.se/annons/3188830' 
}

0 个答案:

没有答案