我有一个使用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);
};
};
其中一个参数传递给getDistance不是你所期望的。让你的userValue函数console.log lat,long,postLat,postLong,看看你有什么。我的猜测是其中的一件事是未定义的。 – James
@詹姆斯刚刚用console.log运行它,每个值都变好了=/ – notJenny
有趣。一个[快速测试](https://jsfiddle.net/6n4jyctz/)使用漂亮乏味的经纬度值对我来说确实很好。我不知道这个bug是否与浏览器版本有关。 – James