在Javascript中检测object是否为null-为什么不起作用?

时间:2019-12-26 08:14:44

标签: javascript

我试图弄清楚为什么我通过JS错误控制台跟踪器从某些访问者那里收到此错误:

  

未捕获的TypeError:无法读取null的属性“ classList”

有问题的代码是:

(channel.cc:851): Changing voice state, recv=1 send=0
(channel.cc:1093): Setting remote video description
(webrtc_video_engine.cc:740): SetSendParameters: {codecs: [VideoCodec[96:VP8]], conference_mode: no, extensions: [{uri: urn:3gpp:video-orientation, id: 13}, {uri: http://www.webrtc.org/experiments/rtp-hdrext/playout-delay, id: 5}], extmap-allow-mixed: false, max_bandwidth_bps: -1, mid: video}
(webrtc_video_engine.cc:685): WebRTC-FlexFEC-03 field trial is not enabled.
(webrtc_video_engine.cc:748): Negotiated codec: VideoCodec[96:VP8]
(webrtc_video_engine.cc:2001): RTX SSRCs configured but there's no configured RTX payload type. Ignoring.
(webrtc_video_engine.cc:2021): RecreateWebRtcStream (send) because of SetCodec.
(balanced_degradation_settings.cc:93): Unsupported size, value ignored.
(alr_experiment.cc:79): Using ALR experiment settings: pacing factor: 1, max pacer queue length: 2875, ALR bandwidth usage percent: 80, ALR start budget level percent: 40, ALR end budget level percent: -60, ALR experiment group ID: 3
(video_send_stream_impl.cc:276): VideoSendStreamInternal: {encoder_settings: { experiment_cpu_load_estimator: off}}, rtp: {ssrcs: [1648984195], rtcp_mode: RtcpMode::kCompound, max_packet_size: 1200, extmap-allow-mixed: false, extensions: [{uri: http://www.webrtc.org/experiments/rtp-hdrext/playout-delay, id: 5}, {uri: urn:3gpp:video-orientation, id: 13}], lntf: {enabled: false}, nack: {rtp_history_ms: 1000}, ulpfec: {ulpfec_payload_type: -1, red_payload_type: -1, red_rtx_payload_type: -1}, payload_name: VP8, payload_type: 96, raw_payload: false, flexfec: {payload_type: -1, ssrc: 0, protected_media_ssrcs: []}, rtx: {ssrcs: [], payload_type: -1}, c_name: 0MWifQdPcdDc99Ky}, rtcp_report_interval_ms: 1000, send_transport: (Transport), media_transport: nullptr, render_delay_ms: 0, target_delay_ms: 0, suspend_below_min_bitrate: off}
(call.cc:1045): UpdateAggregateNetworkState: aggregate_state=down
(rtp_transport_controller_send.cc:282): SignalNetworkState Down
(video_send_stream.cc:160): VideoSendStream::Stop
(webrtc_video_engine.cc:891): SetFeedbackOptions on all the receive streams because the send codec or RTCP mode has changed.
(video_stream_encoder.cc:637): ConfigureEncoder requested.
(video_send_stream_impl.cc:443): VideoSendStream::Stop
(video_send_stream.cc:160): VideoSendStream::Stop
(channel.cc:997): Changing video state, send=0
(video_send_stream_impl.cc:443): VideoSendStream::Stop

这行不通吗

                if (typeof item == "object") {
                    item.classList.remove('active');
                    update_specific_marker(item.getAttribute('data-what'),"remove_active_icon");
                }

我一开始就对如何获得它感到困惑-因为我无法从该页面看到任何错误(无论我如何使用它,即使在看起来如此的Chrome中也是如此)来自)

关于我可以尝试的任何建议吗?

2 个答案:

答案 0 :(得分:2)

如果您在JS控制台中执行typeof null,它将打印object。 因此如果您的情况是item = null,那么typeof item == "object"也会产生true

相反,您可以做的是

if(item){
  // do things
}

答案 1 :(得分:1)

对于item = nulltypeof item == "object"为true,因此您要在块内尝试访问空值的classList属性。您不能这样做,因此会出错。

要解决此问题,您将需要另一个条件来阻止空值访问代码。

if (typeof item == "object" && item != null)