简体   繁体   English

在Meanjs应用程序控制器中承诺“那么”不是一个功能

[英]Promise in meanjs app controller “then” is not a function

In my meanjs app created using meanjs generator and auto generated CRUD-module, for income below, I want to customize the promise returned by the get method so that I can call methods and do transformations on the retrieved data. 在我的使用meanjs生成器和自动生成的CRUD模块创建的meanjs应用程序中,为了获得下面的收入,我想自定义get方法返回的promise,以便我可以调用方法并对检索到的数据进行转换。 So i attempt to change it in my controller: 所以我尝试在我的控制器中更改它:

// Find existing Income
    $scope.findOne = function() {
        Incomes.get({
            incomeId: $stateParams.incomeId
    }).then(function(income){
       $scope.income = income;
       $scope.incomeDollar= income*$scope.dollarRate;
    });

This only gives me the error : undefined is not a function, and pointing on then. 这只会给我错误:undefined不是一个函数,然后指向。 What am i doing wrong here? 我在这里做错了什么? How can I do transformations on the data retrieved from the get method above? 如何对从上述get方法检索的数据进行转换?

The pattern i am trying to use is in the end of the article here . 我想使用的模式是在文章的末尾位置 I want to switch from the short version to the long so that i can do more stuff in my callback. 我想从短版本切换到长版本,以便在回调中做更多的事情。

// LONG:

myModule.controller('HelloCtrl', function($scope, HelloWorld) {

  HelloWorld.getMessages().then(function(messages) {
    $scope.messages = messages;
  });

});

To simplify this, the writer of the article place the promise returned by 'getMessages' on the scope: 为了简化此过程,本文的作者将“ getMessages”返回的承诺放在范围内:

// SHORTER:

myModule.controller('HelloCtrl', function($scope, HelloWorld) {

  $scope.messages = HelloWorld.getMessages();

});

The promise object can be accessed using $promise. 可以使用$ promise访问promise对象。 From the doc 文档

The Resource instances and collection have these additional properties: 资源实例和集合具有以下附加属性:

$promise: the promise of the original server interaction that created this instance or collection. $ promise:创建此实例或集合的原始服务器交互的承诺。

On success, the promise is resolved with the same resource instance or collection object, updated with data from server... 成功后,将使用相同的资源实例或集合对象来解决承诺,并使用服务器中的数据进行更新...

So, your code should look like: 因此,您的代码应如下所示:

// Find existing Income
$scope.findOne = function() {
    Incomes.get({
        incomeId: $stateParams.incomeId
}).$promise.then(function(income){
   $scope.income = income;
   $scope.incomeDollar= income*$scope.dollarRate;
});

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

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