【问题标题】:Express.js REST API RxJS retryWhen is not a functionExpress.js REST API RxJS retryWhen 不是函数
【发布时间】:2018-05-04 18:26:39
【问题描述】:

我在 node 和 express 中制作了 REST API。看起来运算符没有被导入(我尝试了几个运算符),我不知道为什么它会在async-db.js 上引发错误。我已经尝试过,以我能想到的所有方式导入运算符,但没有成功,我还安装了rxjs-compat,正如您在我的package.json 中看到的那样。 Observable 和 Subject 工作正常,我不知道我做错了什么。我在 Angular 客户端应用程序中多次使用过这样的导入,一切正常。

。 我的文件结构如下:

+ app.js
+ async-db.js
++ routes/routes.js

我的代码如下所示:

import { Observable, Subject, from, of} from 'rxjs';
import { map, retryWhen, delay, retry, retryTime } from 'rxjs/operators';
export function t2tObservable({ db, name, param }) {
  let retryTime = 125;
  let subject = new Subject();
  let dbRef = db.ref(param ? name + "/" + param : name);
  dbRef.once("value", (snap) => {
    if (snap.val()) {
      subject.next(snap.val());
      subject.complete();
    }
    else {
     subject.error(new Error("T2TError: no data"));
    }
   }, (e) => {
     subject.error(e);
     console.log("The read failed: " + e.code);
   });
   return subject.asObservable().retryWhen(function (errors) {
     retryTime *= 2;
     return errors.delay(retryTime);
   });
 }

错误如下所示:

return subject.asObservable().retryWhen(function (errors) {
                            ^
TypeError: subject.asObservable(...).retryWhen is not a function
at Object.t2tObservable (...\t2tauthapi\dist\async-db.js:33:33)
at Object.<anonymous> (...\t2tauthapi\dist\routes\routes.js:36:9)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (...\t2tauthapi\dist\app.js:7:14)

我的 package.json 看起来像这样

{
  "name": "t2tauthapi",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore 
    ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
    "start": "npm run build && node dist/app.js"
  },
  "author": "Toni Beverin",
  "license": "ISC",
  "dependencies": {
  "body-parser": "^1.18.2",
  "cors": "^2.8.4",
  "express": "^4.16.3",
  "firebase-admin": "^5.12.0",
  "jsonwebtoken": "^8.2.1",
  "lodash": "^4.17.10",
  "rxjs": "^6.1.0",
  "rxjs-compat": "^6.1.0"
},
"devDependencies": {
  "babel-cli": "^6.26.0",
  "babel-preset-es2015": "^6.24.1",
  "rimraf": "^2.6.2"
  }
}

【问题讨论】:

    标签: node.js express rxjs


    【解决方案1】:

    rxjs/operators 导入与 pipe 运算符 (https://ncjamieson.com/understanding-lettable-operators/) 一起使用的函数

    您直接在 Observable 上使用的函数,例如 .map.filter.retryWhen... 必须是 added 到 Observables 的原型。

    所以你必须像这样导入retryWhenimport 'rxjs/add/operator/retryWhen';


    但最好使用管道运算符。如果你想使用它,你必须像这样改变功能链:

    来自:

    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/retryWhen';
    
    subject.asObservable()
        .map(...logic)
        .retryWhen(...logic);
    

    到:

    import { map, retryWhen } from 'rxjs/operators';
    
    subject.asObservable().pipe(
        map(...logic),
        retryWhen(...logic));
    

    【讨论】:

      【解决方案2】:

      看起来它无法从rxjs/operators 文件夹中导入运算符,但是当我从rxjs/operator 文件夹中导入它们时它正在工作。在我的 node_modules 文件夹中,两个文件夹中都有相同的运算符,我不确定为什么会这样,但它可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-28
        • 2017-03-01
        • 2020-02-26
        • 2019-05-20
        • 2016-12-25
        • 1970-01-01
        相关资源
        最近更新 更多