LoginSignup
0
0

More than 3 years have passed since last update.

vue.js length error isset

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