0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vue.js length error isset

0
Posted at

vue.jsで length したい場合がある。
しかし、エラーが出る。

そんな時は、 undefined チェックしてからやればいい。


            //ユーザーのアイコンを取得
            getIcon(user){

                let icon = '';

                icon = user.img;

                if (typeof user.socials !== 'undefined') {
                    // your code here
                    if(user.socials.length != 0){
                        icon = user.socials[0].img;
                    }
                }

                if (typeof user.photos !== 'undefined') {
                    // your code here
                    if(user.photos.length != 0){
                        icon = "/gcp_storage/photo_300x300_" + user.photos[0].id + ".jpg";
                    }
                }

                return icon;

            },

こんな感じ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?