javascript 未捕获的类型错误:无法调用未定义的方法“推送”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15673932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:38:16  来源:igfitidea点击:

Uncaught TypeError: Cannot call method 'push' of undefined

javascriptgoogle-chromegoogle-maps-api-3syntax-error

提问by SpencerChantler123

I have been stuck at this error for days, can any kind soul decipher this error? The alert shows the correct data that is needed but somehow the push() method just doesn't work.. Thanks in advance!

我已经被这个错误困了好几天了,有没有好心人能破译这个错误?警报显示了所需的正确数据,但不知何故 push() 方法不起作用.. 提前致谢!

回答by Karol

It's not really easy to tell, but basing on a comments, the object you're calling pushmethod on is probably undefined. And this objectshould be an array.

这并不容易分辨,但根据评论,您正在调用push方法的对象可能是undefined. 这object应该是一个array.

Replace this line:

替换这一行:

layer[result['layerId']].push(result);

With following code:

使用以下代码:

if("undefined" != typeof layer[result['layerId']]) {
    layer[result['layerId']].push(result);
}
else {
    layer[result['layerId']] = new Array();
    layer[result['layerId']].push(result);
}

Let me know if it works.

让我知道它是否有效。