简体   繁体   English

环回Geopoint挂钩未持久更改

[英]Loopback Geopoint Hook not persisting change

I created a before save hook in my loopback api app. 我在回送api应用程序中创建了一个保存前的钩子。 My location model has an address and a latlng geopoint 我的位置模型有一个地址和一个latlng地理位置

  "properties": {
    "name": {
      "type": "string"
    },
    "address": {
      "type": "string"
    },
    "latlng": {
      "type": "geopoint"
    }
  },

Within my common\\models\\location.js I have the following code: 在我的common\\models\\location.js我有以下代码:

var https = require('https');
var loopback = require('loopback');
module.exports = function(Location) {
  Location.observe('before save', function(ctx, next) {
    let path = `/maps/api/geocode/json?address=${ctx.instance['address']}&key=AIzaSyCWk3ePB8idTw74LyhR8tLSCmVgbZDKiIQ`
    console.log(path);
    https.get({
      hostname: 'maps.googleapis.com',
      path: encodeURI(path)
    }, (res) => {
      var body = [];
      res.on('data', (d) => {
        body.push(d)
      })
      res.on('end', () =>{
        body = JSON.parse(Buffer.concat(body).toString());
        let latlng = new loopback.GeoPoint({
          lat: body['results'][0]['geometry']['location']['lat'],
          lng: body['results'][0]['geometry']['location']['lng']
        })
        console.log(latlng)
        if (ctx.instance) {
          ctx.instance.latlng = latlng;
        } else {
          ctx.data.latlng = latlng;
        }
      })
    })
     next();
  })
};

What I'm expecting this code to do is to make an http(s) request to google maps, return the latitude and longitude of the address, naively predict the first entry correct and persist that data to my model in the latlng property. 我期望这段代码要做的是向Google地图发出一个http(s)请求,返回地址的纬度和经度,天真地预测出第一个正确的条目,并将该数据保留在latlng属性中的模型中。 I am currently correctly outputting to the console the expected latitude and longitude as a GeoPoint object: 我目前正在正确地将期望的纬度和经度作为GeoPoint对象输出到控制台:

GeoPoint { lat: 42.3675294, lng: -71.18696609999999 }

How can I update my model with this information? 如何使用此信息更新模型? I've tried to remove the conditional if (ctx.instance) with no difference. 我试图删除无条件的条件if (ctx.instance) I've tried using a different object notation: 我尝试使用其他对象表示法:

ctx.instance['latlng']=latlng

No errors are being logged. 没有错误被记录。 I'm persisting with MongoDB, but the problem is the same using in-memory db. 我坚持使用MongoDB,但使用内存数据库的问题相同。 Additional code located on Github.com/andygauge/api.socialconnect 附加代码位于Github.com/andygauge/api.socialconnect

The problem here is the placement of next() . 这里的问题是next()的位置。 Due to the asynchronous nature of nodejs, next() was called before the http request completed and processed. 由于nodejs的异步特性,因此在http请求完成和处理之前调用next() The changes to the model occur after the workflow proceeds to validation. 在工作流程进行验证之后,将对模型进行更改。

The solution looks like this: 解决方案如下所示:

module.exports = function(Location) {
  Location.observe('before save', function(ctx, next) {
    let path = `/maps/api/geocode/json?address=${ctx.instance['address']}&key=AIzaSyCWk3ePB8idTw74LyhR8tLSCmVgbZDKiIQ`
    console.log(path);
    https.get({
      hostname: 'maps.googleapis.com',
      path: encodeURI(path)
    }, (res) => {
      var body = [];
      res.on('data', (d) => {
        body.push(d)
      })
      res.on('end', () =>{
        body = JSON.parse(Buffer.concat(body).toString());
        let latlng = new loopback.GeoPoint({
          lat: body['results'][0]['geometry']['location']['lat'],
          lng: body['results'][0]['geometry']['location']['lng']
        })
        console.log(latlng)
        if (ctx.instance) {
          ctx.instance.latlng = latlng;
        } else {
          ctx.data.latlng = latlng;
        }
        next();
      })
    })
  })
};

Thank you for the views. 感谢您的意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM