Vue.js Cannot read property 'length' of null

半城伤御伤魂 提交于 2021-02-07 12:19:44

问题


If I do this:

<div class="panel panel-default" v-if="socialiteLogins !== null">

The panel doesn't hide. If I check socialiteLogins === null alone, or with ==, they both return that the object isn't null. It's definitely null though. If I dump it on the page, I get [] as the result. It's an empty json object. So if I try this:

<div class="panel panel-default" v-if="socialiteLogins.length !== 0">

The panel still doesn't hide and I get this error:

Cannot read property 'length' of null

But If I do this:

<div class="panel panel-default" v-if="socialiteLogins !== null && socialiteLogins.length !== 0">

It hides the panel perfectly with no warnings on initial load but when I update the socialiteLogins variable later I get the length warning if it ever returns an empty json object again. Any idea why?

Edit:

Adding to it... if I do this:

<div class="panel panel-default" v-show="socialiteLogins">

It shows on initial load even though there are none, but if I remove them after the page loads it properly hides the panel. So the only issue appears to be the initial loading where it's not properly detecting that there are no records.


回答1:


I am no expert on Vue.js, but the following applies to JS in general.


If socialiteLogins is null or undefined, you can't read the length property of it. That only can be read if socialiteLogins is an array, object, or function. That is why you get the message:

Cannot read property 'length' of null

If socialiteLogins is undefined or an empty array, socialiteLogins !== null. However, socialiteLogins == null (Note that this is using loose comparison).

If socialiteLogins is an empty array, it is still truthy. v-show will regard it as true.

The combination of these facts is making your code not work.


In your case, I think this will work:

 <div class="panel panel-default" v-show="socialiteLogins && socialiteLogins.length">

How it works:

The JS && operator evaluates the first statement; if it is truthy, it returns the value of the second statement. If the first statement is falsy, it returns its value.

v-show coerces the result of the expression to a boolean.

If socialiteLogins is undefined or null, it returns that value, which is coerced to false.

If socialiteLogins is an empty array, socialiteLogins is truthy, so && returns the second statement; socialiteLogins.length will be 0. That will be coerced to false as well.

If socialiteLogins is an array with contents, socialiteLogins will be truthy, and socialiteLogins.length will be a non-zero number, which will be coerced to true.




回答2:


@RyanZim's answer helped. Here's the solution in case anyone else comes here by search in the future.

The issue arises from the initial state of the data. For instance, I had this:

data: function() {
    return {
        socialiteLogins: null
    }
},

Which works for !== null but not for checking .length. Later when it returns an empty object, .legnth will work but not null.

So the solution is keeping it the proper type the entire time so that I can run a consistent check:

data: function() {
    return {
        socialiteLogins: {}
    }
},

Edit: for anyone finding this answer in the future, the main issue here is not using the right type. If you want to check for length, you have to use an array. For objects, you would count the keys, not the length which isn't a property of objects.




回答3:


Here is the tip for you.

<div class="panel panel-default" v-if="Object.keys(socialiteLogins).length > 0">

and here we go that's all.

mark as answered.



来源:https://stackoverflow.com/questions/40753964/vue-js-cannot-read-property-length-of-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!