TypeError:无法读取Geolib.isDecimal中未定义的属性'toString'

时间:2017-10-19 20:15:23

标签: javascript node.js push-notification firebase-cloud-messaging

我有一个使用Firebase云功能的推送通知系统,最近我注意到推送通知不再出现(他们过去几个月一直在工作)。当我进入日志时,我正在看

TypeError: Cannot read property 'toString' of undefined
at Geolib.isDecimal   (/user_code/node_modules/geolib/dist/geolib.js:1326:26)
at Geolib.useDecimal (/user_code/node_modules/geolib/dist/geolib.js:1241:22)
at Geolib.coords (/user_code/node_modules/geolib/dist/geolib.js:179:85)
at Geolib.getDistance (/user_code/node_modules/geolib/dist/geolib.js:247:26)
at userValue (/user_code/index.js:104:34)
at /user_code/index.js:94:4
at /user_code/node_modules/firebase-admin/lib/database/database.js:114:356
at Xb.h.ha (/user_code/node_modules/firebase-admin/lib/database/database.js:50:275)
at Xb.h.ha (/user_code/node_modules/firebase-admin/lib/database/database.js:50:268)
at Xb.h.ha (/user_code/node_modules/firebase-admin/lib/database/database.js:50:310)

我没有更改任何代码,并重新下载Geolib但仍然不会让步,任何帮助将不胜感激。

我的代码:

 exports.postMade = functions.database.ref('/PostData/post/{pushId}').onWrite(event => {

console.log('Someone made a post');

var valueObject = event.data.val();

console.log('the data in valueObject'+ valueObject)

return admin.database().ref('Users').once('value').then(snapshot => {

    snapshot.forEach(function(child){
        var values = child.val();
        console.log('the values inside of value'+ values)
         userValue(values.userLat,values.userLong,values.radiusDistance,values.token,valueObject.lat,valueObject.long,valueObject.userUid,valueObject.text)
    })
});
});


function userValue(lat,long,radiusDistance,token,postLat,postLong,postUser,postText){

var unconvertedDistance = geolib.getDistance({latitude: lat, longitude: long}, {latitude: postLat, longitude: postLong});

var convertedDistance = geolib.convertUnit('mi',unconvertedDistance, 0)

console.log('the values of both unconvertedDistance and convertedDistance'+ unconvertedDistance,+ convertedDistance)

if (convertedDistance <= radiusDistance){
const payload = {
          notification: {
            title: "Someone posted near you!",
            body: postText,
            badge: "1",
            sound: "default"
        },
    };
const options = {
     priority: "high",
        timeToLive: 60 * 60 * 24
    };

return admin.messaging().sendToDevice(token, payload, options);
};
};

3 个答案:

答案 0 :(得分:1)

此geoLib包已知issue,自2016年7月起待定

答案 1 :(得分:0)

确保纬度,经度和半径为字符串。我有同样的问题,我发现它可以正常使用字符串参数

答案 2 :(得分:0)

class Distance {
  toRadians(degrees) {
    return degrees * (Math.PI / 180);
  }

  calc(from, to) {
   const lat1 = parseFloat(from.lat);
   const lon1 = parseFloat(from.lng);
   const lat2 = parseFloat(to.lat);
   const lon2 = parseFloat(to.lng);
   const R = 6371; // Radius of the earth in km
   const dLat = this.deg2rad(lat2 - lat1); // deg2rad below
   const dLon = this.deg2rad(lon2 - lon1);
   const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
   Math.cos(this.deg2rad(lat1)) * Math.cos(this.deg2rad(lat2)) *
   Math.sin(dLon / 2) * Math.sin(dLon / 2);
   const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
   const d = R * c; // Distance in km
   return d;
 }

 deg2rad(deg) {
   return deg * (Math.PI / 180);
 }
 }

export default new Distance();