Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 用AngluarJS将数据从数据库服务传递到控制器_Angularjs_Angular Promise_Pouchdb - Fatal编程技术网

Angularjs 用AngluarJS将数据从数据库服务传递到控制器

Angularjs 用AngluarJS将数据从数据库服务传递到控制器,angularjs,angular-promise,pouchdb,Angularjs,Angular Promise,Pouchdb,我已经在Angular/Ionic中设置了一个PockDB服务,该服务在服务中运行良好,但当我尝试将从PockDB服务检索到的数据传递给控制器时失败 这是我的服务 angular.module('myApp', []) .service('DBService', function($q) { var items; var db; var self = this; this.initDB = function() { return db = new PouchDB('sim

我已经在Angular/Ionic中设置了一个PockDB服务,该服务在服务中运行良好,但当我尝试将从PockDB服务检索到的数据传递给控制器时失败

这是我的服务

angular.module('myApp', [])
.service('DBService', function($q) {
  var items;
  var db;
  var self = this;
  this.initDB = function() {
    return db = new PouchDB('simpleDB', {
      adapter: 'websql'
    });
  };
this.storeData = function() {
    return $q.when(
              db.put({
          _id: 'mydoc',
          title: 'some text'
        }).then(function (response) {
          // handle response
          console.log('done')
        }).catch(function (err) {
          console.log(err); 
        })
      )
  };
this.getData = function(){
  return $q.when(
    db.get('mydoc').then(function (doc) {
    // handle doc
    console.log(doc); // I see the data in console
     }).catch(function (err) {
     console.log(err);
     })
  )
}
})
这是控制器

angular.module('myApp', [])
.controller('mainCtrl', function($scope, DBService, $q) {
  $scope.getData = function(){
      DBService.initDB()
      DBService.getData().then(function(data){ 
        console.log(data)
      })
    }
当我使用then()时,我得到错误类型错误:无法读取未定义的属性“then”


有人能帮我弄清楚如何将数据从我的服务正确地传递到控制器吗

我很幸运,很快就发现了错误!我没有在我的服务中返回数据,所以我将其更改为

this.getData = function(){
  return $q.when(
    db.get('mydoc').then(function (doc) {
    // handle doc
    return doc; // I added this 
    console.log(doc); // I see the data in console
     }).catch(function (err) {
     console.log(err);
     })
  )
}

then()返回undefined的原因是我无意中忽略了return$q.when(从this.getData函数的第二行!现在我在控制器中有了所需的数据!

我很幸运很快发现了错误!我没有在服务中返回数据,所以我将其更改为

this.getData = function(){
  return $q.when(
    db.get('mydoc').then(function (doc) {
    // handle doc
    return doc; // I added this 
    console.log(doc); // I see the data in console
     }).catch(function (err) {
     console.log(err);
     })
  )
}
then()返回undefined的原因是,我无意中忽略了return$q.when(从this.getData函数的第二行!现在我在控制器中有了我需要的数据