Promise does not execute . then()

Asked

Viewed 28 times

2

Good morning, I need help with my Password, when recording my information in indexDB I am using . then to write on the console, but this . then is not fired.

I noticed that my Promise gets pending status and I don’t know if it has to be.

//codigo executado no console

const negociacao = new Negociacao( new Date(), 7, 100);

ConnectionFactory
                .getConnection()
                .then(conn => new NegociacaoDAO(conn))
                .then(dao => dao.adiciona(negociacao))
                .then(() => console.log('sucess'))
                .catch(err => console.log(err));

the Tradeable class is the class responsible for adding the items in the indexDB and returning this class

class NegociacaoDAO{

    constructor(connection){

        this._connection = connection;
        this._store = 'negociacoes';

    }

    adiciona(negociacao){

        return new Promise( (resolve, reject)=>{
            const request = this._connection
                                        .transaction([this._store], 'readwrite')
                                        .objectStore(this._store)
                                        .add(negociacao);

            request.onsucess = e => resolve(e.target.result);

            request.onerror = e => {
                console.log(e.target.error);
                reject('Não foi possivel salvar a negociação');
            }
        });

    }

  • request.onsucess would not be request.onsuccess ?

  • Thank you Artur, I’m a beginner and since yesterday I have this problem.

  • No problem. If this solved your problem, be sure to accept the answer.

1 answer

3


I believe the problem lies in this line:

request.onsucess = e => resolve(e.target.result);

It is missing a 'c'. Thus the callback will never be called, and the Promise will never be resolved. The correct would be:

request.onsuccess = e => resolve(e.target.result);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.