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;
},
こんな感じ。