简体   繁体   English

TypeError:无法读取未定义的 BehaviorSubject 的属性“next”

[英]TypeError: Cannot read property 'next' of undefined BehaviorSubject

My code is working when I run it and when I did console.logs.当我运行它和执行 console.logs 时,我的代码正在运行。 the logs tell that isQrCodeShown$ is not undefined.日志表明isQrCodeShown$不是未定义的。

I'm getting error TypeError: Cannot read property 'next' of undefined我收到错误TypeError: Cannot read property 'next' of undefined

TypeError: Cannot read property 'next' of undefined
    at <Jasmine>
    at SafeSubscriber.setQrShown [as _next] (http://localhost:9876/_karma_webpack_/src/app/modules/review-sign/services/qr-code/qr-code.service.ts:24:25)
    at SafeSubscriber.__tryOrUnsub (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:183:1)
    at SafeSubscriber.next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:122:1)
    at Subscriber._next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:72:1)
    at Subscriber.next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:49:1)
    at MapSubscriber._next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/operators/map.js:35:1)
    at MapSubscriber.next (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Subscriber.js:49:1)
    at Observable._subscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/util/subscribeToArray.js:3:1)
    at Observable._trySubscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Observable.js:42:1)
    at Observable.subscribe (http://localhost:9876/_karma_webpack_/node_modules/rxjs/_esm2015/internal/Observable.js:28:1)

qr-code.service.ts二维码.service.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { QR_ALLOWED_COUNTRIES } from '../../configs/qr-code.config';

@Injectable()
export class QrCodeService {

  isQrCodeShown$: BehaviorSubject<boolean>;

  constructor() {
    this.isQrCodeShown$ = new BehaviorSubject(false);

    const MOCK_DATA_SERVICE = of({ country: 'PH' });

    MOCK_DATA_SERVICE
      .pipe(
        map(user => user.country)
      )
      .subscribe(this.setQrShown);
  }

  setQrShown(country: string): void {
    this.isQrCodeShown$.next(QR_ALLOWED_COUNTRIES.includes(country));
  }
}

qr-code.service.spec.ts二维码.service.spec.ts

import { TestBed } from '@angular/core/testing';
import { Subscription } from 'rxjs';
import { QR_TEST_COUNTRY } from '../../configs/qr-code.config';

import { QrCodeService } from './qr-code.service';

describe('QrCodeService', () => {
  let service: QrCodeService;

  beforeEach(() => {
    spyOn(console, 'log').and.callThrough();
    TestBed.configureTestingModule({
      providers: [QrCodeService]
    });
    service = TestBed.inject(QrCodeService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('isQrCodeShown$ should be false when the given country is not on the list', done => {
    const MOCK_FALSE_COUNTRY = 'ATLANTIS';

    service.setQrShown(MOCK_FALSE_COUNTRY);
    service.isQrCodeShown$.subscribe(isShown => {
      expect(isShown).toBeFalsy();
      done();
    });
  });

  it('isQrCodeShown$ should be true when the given country is on the list', done => {
    service.setQrShown(QR_TEST_COUNTRY);
    service.isQrCodeShown$.subscribe(isShown => {
      expect(isShown).toBeTruthy();
      done();
    });
  });
});

This is when I add the two tests using the BehaviorSubject service.isQrCodeShown$这是我使用 BehaviorSubject 服务添加两个测试的时候service.isQrCodeShown$

  • isQrCodeShown$ should be false when the given country is not on the list当给定国家不在列表中时,isQrCodeShown$ 应该为 false
  • isQrCodeShown$ should be true when the given country is on the list当给定国家在列表中时,isQrCodeShown$ 应该为真

I also test it using the ff command ng test --include file-path/file.spec.ts我还使用 ff 命令ng test --include file-path/file.spec.ts测试它

setQrShown isn't an arrow function, and here setQrShown不是箭头 function,这里

//...
    .subscribe(this.setQrShown);

the this context is lost. this上下文丢失了。 You can either be more verbose:您可以更详细:

    .subscribe(value => this.setQrShown(value));

or make setQrShown an arrow function:或使setQrShown成为箭头 function:

setQrShown = (country: string): void => {

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

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