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';

6
  • 1
    You're playing with Promises and returning this.data before promise is fulfilled - hence the result Commented Mar 24, 2017 at 9:00
  • ^^ That and you should be setting it with me.data = result, since this has different meaning in the function where you set the result. Commented Mar 24, 2017 at 9:01
  • how do i make it wait, if i do this, Commented Mar 24, 2017 at 9:01
  • 1
    Possible duplicate of How do I return the response from an asynchronous call? Commented Mar 24, 2017 at 9:02
  • Put the return statement inside then Commented Mar 24, 2017 at 9:03

2 Answers 2

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);
    });  
}
Sign up to request clarification or add additional context in comments.

Comments

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);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.