我收到这种奇怪的行为,其中一个对象的键值对的输出被认为是未定义的,尽管它明确地在对象中被定义并被打印出来。
我的代码如下所示:
public ResizeableWindow()
{
InitializeComponent();
}
private void HelpKeyListen(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1)
{
var HelpControl = new Help();
DockPanel.SetDock(HelpControl, Dock.Right);
HelpPanel.Children.Insert(0, HelpControl);
}
}
浏览器中addLocation(loc: any) {
this.getLocationGPSFromFirebase(loc.$key).then(result => loc = Object.assign(loc, result));
console.log(loc);
this.addLocationToLocalArray(loc);
}
的输出是:
对象{地址:“blablabla”,城市:对象
console.log
我创建对象和随后推送到数组的代码:
{address: "blablabla"…}
address: "blablabla"
latitude: 37.412368
longitude: 6.0755334
$exists: function ()
$key: "uniqueKeyFromFirebase"
__proto__: Object
addLocationToLocalArray(loc: Location) {
console.log(loc.latitude, loc.longitude);
this.tourLocs.push({
address: loc.address,
latitude: loc.latitude,
longitude: loc.longitude
});
}
的输出是:
console.log(loc.latitude, loc.longitude);
当我打印数组时,它看起来像这样:
undefined, undefined
这绝对是奇怪的行为,我不知道是什么导致它。
答案 0 :(得分:1)
在从getLocation api获得响应之前,您正在调用add函数。所以你应该在回调api中调用你的方法。
this.getLocationGPSFromFirebase(loc.$key)
.then(result =>
loc = Object.assign(loc, result)
console.log(loc);
this.addLocationToLocalArray(loc);
);