简体   繁体   English

在angular.js + node.js / electron中的承诺不起作用

[英]Promise in angular.js + node.js/electron doesn't work

I'm using angular.js in electron along with node-orm to talk to a database. 我在电子中使用angular.js以及node-orm与数据库进行通讯。 Node-orm find/get functions are async so I tried to use Promises to get data in a service like so: Node-orm的查找/获取功能是异步的,因此我尝试使用Promises来获取服务中的数据,如下所示:

app.service('SearchService', function($q) {
  this.title = function(token) {
    var deferred = $q.defer();
    Unit.find({}).where("unit_title LIKE ?", ['%'+token.toUpperCase()+'%']).run(function(err, results) {
      if (err) {
        return console.error('error running title query', err);}
      deferred.resolve(results);
    });
    return deferred.promise;
  }
});
app.controller("GreetController", function($scope, SearchService) {
  $scope.units = SearchService.title('test');
});

With the goal of angular translating the promise in the view itself.: 以角度平移视图本身中的诺言为目标:

<div ng-controller="GreetController">
<ul>
    <li ng-repeat="unit in units">{{unit.title}}</li>
</ul>
</div>

However it doesn't work. 但是,它不起作用。 I know the Promises resolve since I can log them to the console and view the values with Chromium's dev tools. 我知道Promises解决了,因为我可以将它们记录到控制台并使用Chromium的dev工具查看值。

Promises are still asynchronous operations, so title method returns a promise object, not actual results . Promise仍然是异步操作,因此title方法返回一个Promise对象,而不是实际results You need to use promise then-able API to provide callbacks that will be called when data is available: 您需要使用promise then-able API提供在数据可用时将调用的回调:

app.controller("GreetController", function($scope, SearchService) {
  SearchService.title('test').then(function(data) {
      $scope.units = data;
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM