Search code examples
angulartypescriptionic2ionic3

How to return value of promise using angular 2


This is my promise function i need to return the value of rs.rows.item(0);

     public getCustomer()  : any
  {
        let  db =  window.sqlitePlugin.openDatabase({name: 'data.db', location: 'default'});
        return new Promise((resolve, reject) =>
        {
            db.transaction(function(tx)
            {
                tx.executeSql('SELECT * FROM customer ORDER BY customerId DESC LIMIT 1', [], function(tx, rs)
                {
                     return resolve(rs.rows.item(0));
                }, 
                function(tx, error) 
                {
                    console.log('SELECT error: ' + error.message);
                    reject(error);
                });
            });
        });    
  }

the return value i got an object like this image image result

i need to get like this example

var customer = getCustomer();
customer.name;
customer.email;

Solution

  • first, you need func to get all your data:

    getAll(): Promise<Phrase[]> {
        return phrasesPromise;
    }
    

    second, if you need one item you can use

    ngOnInit() {
        this.phraseService
            .getAll()
            .then((result: Phrase[]) => this.phrases = result);
    }