【发布时间】:2021-01-07 17:25:39
【问题描述】:
我使用 Svelte 一周,在我的应用程序上使用 cidaas-sdk 进行身份验证后,几秒钟后不显示配置文件数据。页面卡在“...等待”,控制台出现这个错误Uncaught (in promise) TypeError: Cannot read property 'profile' of undefined at Array.create_if_block$1 at create_then_block$2
这里的问题是我必须刷新页面才能显示数据。
我的问题是我无法在登录后直接获取{userInfo.profile.given_name},所以重新加载页面以显示数据是没有用的。
但是当页面重新加载时,一切正常,控制台中没有错误消息。
import cidaas from './helpers/cidaas';
import Profile from './components/profile.svelte';
import { onMount } from 'svelte';
export let title;
$ : isAuth = false;
$ : isLoaded = false;
let promiseHome = Promise.resolve([]);
onMount(async () => {
promiseHome = cidaas.getUserInfo();
promiseHome.then(userInfo => {
if(!userInfo) {
// if you are logged
if (window.location.href.includes("grant_type=login")) {
cidaas.loginCallback();
window.history.replaceState("", "", "/");
isAuth = true;
isLoaded = true;
return;
}
}
isAuth = !!userInfo;
isLoaded = true;
return;
})
});
HTML 部分:
{#await promiseHome}
<div class="mt-5">...Waiting</div>
{:then userInfo}
{#if isAuth == true}
<div class="jumbotron col-md-8 mx-auto">
<div class="hello mt-5 mb-3">Hello {userInfo.profile.given_name}!</div>
<p>isAuth : {isAuth}</p>
<p>isLoaded : {isLoaded}</p>
</div>
<Profile/>
{:else}
<h1 class="mt-5">Bienvenue sur {title}</h1>
<p>isAuth : {isAuth}</p>
<p>isLoaded : {isLoaded}</p>
<button class="btn btn-primary btn-mdir" on:click={loginTo}>Se connecter</button>
<button class="btn btn-primary btn-mdir" on:click={register}>S'inscrire</button>
{/if}
{:catch error}
<p>{error.message}</p>
{/await}
【问题讨论】:
-
您能否在Svelte REPL 中进行最小限度的复制?我不确定
cidaas.getUserInfo()会返回什么。
标签: javascript promise svelte