1

我有一個使用Firebase雲功能的推送通知系統,最近我注意到推送通知不再進入(他們一直在爲過去工作幾個月)。當我進入日誌我看到TypeError:無法讀取未定義的屬性'toString'在Geolib.isDecimal

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); 
}; 
}; 
+0

其中一個參數傳遞給getDistance不是你所期望的。讓你的userValue函數console.log lat,long,postLat,postLong,看看你有什麼。我的猜測是其中的一件事是未定義的。 – James

+0

@詹姆斯剛剛用console.log運行它,每個值都變好了=/ – notJenny

+0

有趣。一個[快速測試](https://jsfiddle.net/6n4jyctz/)使用漂亮乏味的經緯度值對我來說確實很好。我不知道這個bug是否與瀏覽器版本有關。 – James

回答

1

它被稱爲issue這個geoLib包,其未決自2016年7月

+1

只是我的運氣,感謝您的幫助 – notJenny

0

確保緯度,經度和半徑是字符串。我有同樣的問題,我發現它工作正常與字符串參數

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(); 
相關問題