この記事について
| 環境 | LIFF(LINE Front-end Framework)+ javascript |
|---|---|
| 目的 | liff.getProfile()の値を取得する |
| 結果 | then()で完了を待ってから取得するようにして解決 |
事象
下記のコードでprofileの中身が[object Promise]になって正しい値が取得できない
profile = liff.getProfile();
原因
liff.getProfile()はPromiseを返す非同期APIなので、戻り値を変数にそのまま代入しても中身はまだ入っていない([object Promise]のまま)。
対処
thenで処理を待ってから値を取得
liff.getProfile().then(function (profile) {
// do something with profile
const aaa = profile.userId;
const bbb = profile.displayName;
alert(aaa + bbb);
}).catch(function (error) {
// handle error
});
補足
async関数の中であれば await でも書ける。
const profile = await liff.getProfile();
