1

This is strangest thing, i ever came across. In Sencha Touch i define a new class and some methods in it. Now the problem is with the getAll method i want to return the result in then fuction but it is not returning that results when i console it, it shows the result. What seems to be the issue, I think its private function but how do i make it public.

When i create new instance of the pouch, It does not return the results i desired

Ext.define('Inertia.view.Pouch', {
    config:{
        database: 'Sencha',
        db: false,
        result: false,
    },

    constructor : function(config) {
        if(config){
            this.setDatabase(config);
        }
        this.setDb(//);
        this.initConfig(config);
    },
    // get All results
    getAll: function(){
        var me = this;
        me.data = 'NO Record found';
       var res = me.getDb().allDocs({
          include_docs: true,
          attachments: true
        }).then(function (result) {

             **// I want this return to be returned when i call getAll()**
             me.data = result;

            // i even tried this
return result;// but its also not working

        }).catch(function (err) {

          console.log(err);

        });

        return  me.data;
    }

});

and when i do this

var p = new Ext.create('test.view.Pouch', 'sencha');
var data = p.getAll();

it shows the

'NO Record found';

Waqar Haider
  • 929
  • 10
  • 33

2 Answers2

0

The only way you have is to return the promise and manage the datas out of the function. The then is run in async, so data will be defined only inside of it.

getAll: function(){
        var me = this;
        var res = me.getDb().allDocs({
            include_docs: true,
            attachments: true
        });
        return  res ;
    }
});


var p = new Ext.create('test.view.Pouch', 'sencha');
var data,
    datapromise = p.getAll();
    datapromise.then(function (result) {
         data = result;
         // do your stuff here
    }).catch(function (err) {
          data='NO Record found';
          console.log(err);
    });
LellisMoon
  • 4,810
  • 2
  • 12
  • 24
0

I've mentioned that your problem was with Promises - you returned something from a function before a Promise had a chance to fulfill - edited your code a bit, try it

// get All results
getAll: function(){
    var me = this;
    me.data = 'NO Record found';
   var res = me.getDb().allDocs({
      include_docs: true,
      attachments: true
    }).then(function (result) {
         // Possible check results here if no records are found.
         me.data = result;
         return result

    }).catch(function (err) {
      // Handle error here 
      console.log(err);
    });  
}
Krzysztof Borowy
  • 538
  • 1
  • 5
  • 21