我有一个功能:
getCoordinates: function() {
geoLocation.getCurrentLocation().then(function(location) {
return "latitude: " + location.latitude + " longitude:" + location.longitude;
});
}
返回undefined,但是当我改为:
getCoordinates: function() {
geoLocation.getCurrentLocation().then(function(location) {
console.log("latitude: " + location.latitude + " longitude:" + location.longitude);
});
}
并运行我得到的相同功能:
“纬度:4X.XXXXXX经度:-12X.XXXXXXX”
我不明白为什么在必须定义数据时它返回undefined,或者它不会记录到控制台。这是某种时间问题吗?我错过了什么?
答案 0 :(得分:4)
您只是return
来自then
回调,而不是来自getCoordinates
函数(事实上它不return
,因此undefined
)。
getCoordinates: function() {
return geoLocation.getCurrentLocation().then(function(location) {
// ^^^^^^
return "latitude: " + location.latitude + " longitude:" + location.longitude;
});
}