Горячий я могу использовать возврат localforage - PullRequest
0 голосов
/ 29 апреля 2020

В моем Vue SPA я использую localforage для хранения токена Bearer.

В компоненте мне нужен токен для загрузки файлов в API.

Я пытался получить токен localforage:

            let token = localforage.getItem('authtoken')
            console.log(token)

Работает, но в результате получается объект обещания:

Promise

result: "eyJ0eXAiOiJKV1QiyuIiwiaWF0IjoxNTg4MTUzMTkzLCJleHAiOjE1ODg…"

status: "resolved"

Когда я пытаюсь console.log(token.result), возвращается null

Как я могу получить доступ к токену?

1 Ответ

1 голос
/ 29 апреля 2020

Официальная документация предусматривает три различных подхода к чтению значений из хранилища.

localforage.getItem('authtoken').then(function(value) {
    // This code runs once the value has been loaded
    // from the offline store.
    console.log(value);
}).catch(function(err) {
    // This code runs if there were any errors
    console.log(err);
});

// Callback version:
localforage.getItem('authtoken', function(err, value) {
    // Run this code once the value has been
    // loaded from the offline store.
    console.log(value);
});

// async/await
try {
    const value = await localforage.getItem('authtoken');
    // This code runs once the value has been loaded
    // from the offline store.
    console.log(value);
} catch (err) {
    // This code runs if there were any errors.
    console.log(err);
}
...