【问题标题】:How to add to a mongo object and return it如何添加到 mongo 对象并返回它
【发布时间】:2016-11-08 20:46:14
【问题描述】:

我正在尝试编写一个获取一堆场地的猫鼬查询并执行谷歌查询以获取到这些场地的距离,并在返回它们之前将该距离添加到每个场地对象。 以下是相关代码(coffeescript):

Venue.find
  .then (dbResponse) ->
    venues = dbResponse
    googleParam = ''
    console.log venues
    venues.forEach (venue) ->
      googleParam += venue.streetAddress + '+' + venue.city + '+' + venue.state + '|'
    request
      url: "https://maps.googleapis.com/maps/api/distancematrix/json?origins=Times+Sq+New+York+NY&destinations=" + googleParam + "&key=" + process.env.GOOGLE_MAPS_API_KEY
      json: true
    .then (response) ->
      venues = _.map venues, (venue, index) ->
        console.log index
        venue.distance = response.rows[0].elements[index].distance.text
        console.log venue.distance
        venue

      res.json venues

当我 console.log place.distance 我得到正确的值,但响应仍然没有距离,只有旧的场地对象。

【问题讨论】:

    标签: node.js mongodb mongodb-query


    【解决方案1】:

    想通了,我不得不在我的查询中调用 .lean() 以使响应成为一个普通的 js 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 2015-02-13
      • 2011-11-18
      相关资源
      最近更新 更多