函数don'中的javascript函数;t返回数据

函数don'中的javascript函数;t返回数据,javascript,function,extjs,sencha-touch,pouchdb,Javascript,Function,Extjs,Sencha Touch,Pouchdb,这是我遇到的最奇怪的事情。在SenchaTouch中,我定义了一个新类和其中的一些方法。现在问题在于getAll方法 我想在然后函数中返回结果,但结果不是返回的 当iconsoleit时,它会显示结果。问题似乎是什么,我认为它的私人功能,但我如何将其公开 当我创建邮袋的新实例时,它不会返回我想要的结果 Ext.define('Inertia.view.Pouch', { config:{ database: 'Sencha', db: false,

这是我遇到的最奇怪的事情。在SenchaTouch中,我定义了一个新类和其中的一些方法。现在问题在于
getAll
方法 我想在
然后
函数中返回结果,但结果不是
返回的
当i
console
it时,它会显示结果。问题似乎是什么,我认为它的私人功能,但我如何将其公开

当我创建邮袋的新实例时,它不会返回我想要的结果

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

当我这么做的时候

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

“未发现任何记录”


唯一的方法是返回承诺并管理函数外的数据。 然后以异步方式运行,因此数据将仅在其中定义

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

唯一的方法是返回承诺并管理函数外的数据。 然后以异步方式运行,因此数据将仅在其中定义

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

我提到过,您的问题在于承诺-在承诺有机会实现之前,您从函数返回了一些东西-稍微编辑一下代码,试试看

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

我提到过,您的问题在于承诺-在承诺有机会实现之前,您从函数返回了一些东西-稍微编辑一下代码,试试看

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

您正在玩承诺游戏,并在承诺兑现之前返回
this.data
-因此结果^^^,您应该使用
me.data=result
设置它,因为
在您设置结果的函数中具有不同的含义。如果我这样做,如何让它等待,可能重复将
return
语句放入
中,然后
您正在玩承诺游戏,并在承诺兑现之前返回
此.data
-因此结果^^^,您应该使用
me.data=result
设置它,由于
在设置结果的函数中具有不同的含义。如何使其等待,如果我这样做,则可能会将
return
语句重复放入
中,然后