【问题标题】:Cannot read property '$$state' of undefined error after AngularJS version upgradeAngularJS 版本升级后无法读取未定义错误的属性“$$state”
【发布时间】:2017-12-10 08:53:45
【问题描述】:

我一直在使用 AngularJS 将我们项目的版本从 1.2.16 升级到 1.6.4。由于我们使用 bower 作为包管理器,因此我更新了 bower.json 的必要部分,如下所示:

{
"name": "takademi",
"version": "0.0.0",
"dependencies": {
  "angular": "1.6.4",
  "json3": "~3.3.1",
  "es5-shim": "~3.1.0",
  "angular-cookies": "1.6.4",
  "angular-sanitize": "1.6.4",
  "angular-animate": "1.6.4",
  "angular-route": "1.6.4",
  "angular-bindonce": "~0.3.1",
  "restangular": "~1.4.0",
  "angular-base64": "~2.0.2",
  "lodash": "~2.4.1"
},
"devDependencies": {
"angular-mocks": "1.2.16",
"angular-scenario": "1.2.16"
},
"appPath": "app"
}

在这次更新之后,我运行了这个项目。现在我可以看到 Angular 的版本为 1.6.4,但出现以下错误:

*** process.env.ENV is not defined, assuming 'prod' env
t @ bundle.js:4776
angular.js:14525 TypeError: Cannot read property '$$state' of undefined
    at then (angular.js:16799)
    at addPromiseLikeThing (angular-busy.js:67)
    at angular-busy.js:22
    at Object.forEach (angular.js:403)
    at Object.tracker.reset (angular-busy.js:18)
    at angular-busy.js:175
    at $watchCollectionAction (angular.js:17859)
    at Scope.$digest (angular.js:17999)
    at Scope.$apply (angular.js:18269)
    at done (angular.js:12387)
(anonymous) @ angular.js:14525
getMarketPlace Failed to load resource: the server responded with a status of 401 (Unauthorized)
angular.js:14525 Possibly unhandled rejection: {"data":{"status":401,"module":0,"code":1000,"message":"AUTHENTICATION_REQUIRED","develo    perMessage":null,"moreInfo":null},"status":401,"config": {"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","params":{"v":967},"headers":{"Accept":"application/json, text/plain, */*"},"url":"/portal/rest/market-place/getMarketPlace"},"statusText":"Unauthorized"}
(anonymous) @ angular.js:14525
(anonymous) @ angular.js:11008
processChecks @ angular.js:16860
$digest @ angular.js:17971
$apply @ angular.js:18269
done @ angular.js:12387
completeRequest @ angular.js:12613
requestLoaded @ angular.js:12541

据我了解,它只是说明我新升级的 AngularJS 1.6.4 文件不支持其中的 $$state 关键字使用。错误来自 Angular 自己的文件,而不是显示我们开发的文件。

这是 chrome 浏览器导航我显示错误的功能 - angular.js 的第 16799 行:

  function Promise() {
this.$$state = { status: 0 };
  }

  extend(Promise.prototype, {
    then: function(onFulfilled, onRejected, progressBack) {
      if (isUndefined(onFulfilled) && isUndefined(onRejected) && isUndefined(progressBack)) {
        return this;
      }
      var result = new Promise();

      this.$$state.pending = this.$$state.pending || [];
      this.$$state.pending.push([result, onFulfilled, onRejected, progressBack]);
      if (this.$$state.status > 0) scheduleProcessQueue(this.$$state);

      return result;
    },

    'catch': function(callback) {
      return this.then(null, callback);
    },

    'finally': function(callback, progressBack) {
      return this.then(function(value) {
        return handleCallback(value, resolve, callback);
      }, function(error) {
        return handleCallback(error, reject, callback);
      }, progressBack);
    }
  });

我在 SO 和 Google 中进行了一些搜索,但我找不到任何相关的帖子或对我的问题的解释。大部分答案都和Ruby有关,或者react:

angular showing TypeError: Cannot read property 'state' of undefined

Uncaught TypeError: Cannot read property 'state' of undefined

react: uncaught TypeError: Cannot read property 'state' of undefined

由于我不了解 AngularJS 和 bower 的全部功能,因此我不清楚这些答案。

你们能帮我解决这个问题吗?

提前致谢!

【问题讨论】:

  • status 401 无法加载资源 "url":"/portal/rest/market-place/getMarketPlace" "statusText":"Unauthorized" 怎么办?
  • 还有什么是angular-busy.js?您使用的是哪个版本? AngularJS 1.6 promise 方法在 6 个月前进行了升级。
  • @georgeawg 我无法深入了解 angular-busy 以了解它到底在做什么,但据我所知,它与 angular 自己的库(github.com/cgross/angular-busy)有关。另一方面,对于您的第一条评论,我认为我应该首先解决 $$state 问题来处理那个问题。因为好像又和angular自带的库有关了。
  • angular-busy 不是由 AngularJS 团队创建的。函数addPromiseLikeThing 看起来很可疑,并且是在进入 $q 库之前执行的最后一件事,它会爆炸。使用“promiseLike”对象伪造 $q 库可能是导致错误的原因。
  • 嘿@georgeawg,我已经调查了 angular-busy,正如你所说,addPromiseLikeThing 函数导致了问题。当我更新我的 angular-busy 时,问题已解决。请随时添加此评论作为答案,以便我接受。

标签: javascript angularjs bower


【解决方案1】:

angular-busy module 不是由 AngularJS 团队创建的。函数addPromiseLikeThing 看起来很可疑,并且是在进入 $q 库之前执行的最后一件事,它会爆炸。使用 'promiseLike' 对象伪造 $q 库可能是导致错误的原因。

此外,AngularJS V1.6 改变了处理拒绝的方式。错误信息:

angular.js:14525 可能未处理的拒绝:{"data":{"status":401,

是这些变化的结果。

更多信息请见AngularJS Developer Guide - Migrating to V1.6 ($q)

【讨论】:

    猜你喜欢
    • 2020-03-30
    • 2020-12-04
    • 1970-01-01
    • 2021-01-06
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    相关资源
    最近更新 更多