错误TypeError:无法在角度自定义管道中读取null的属性“ split”

时间:2019-12-21 14:53:32

标签: angular angular-pipe pipes-filters

我收到此错误ERROR TypeError:使用此角形管道时无法读取null错误的属性'split',这是代码。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'fullDate'
})
export class DatePipe implements PipeTransform {

  transform(value:any ) {
    const dateArray = value.split('-');
    const date = dateArray[2].substr(0, 1) === '0' ? dateArray[2].substr(1, 1) : dateArray[2];
    const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  
    return `${date} ${months[dateArray[1] - 1]} ${dateArray[0]}`;
  }

}

  

{{lorem?.lorem_date | fullDate}}

enter image description here

3 个答案:

答案 0 :(得分:2)

该错误表示您正在拆分一个空值,因此您只需添加一个检查即可。

尝试这样:

sterilizeQ.put(c);

答案 1 :(得分:2)

在管道中添加transform(value:any ) { if(value){ const dateArray = value.split('-'); const date = dateArray[2].substr(0, 1) === '0' ? dateArray[2].substr(1, 1) : dateArray[2]; const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; return `${date} ${months[dateArray[1] - 1]} ${dateArray[0]}`; } } 支票:

null

OR

将内容放入import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'fullDate' }) export class DatePipe implements PipeTransform { transform(value: any) { if (value) { const dateArray = value.split('-'); const date = dateArray[2].substr(0, 1) === '0' ? dateArray[2].substr(1, 1) : dateArray[2]; const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; return `${date} ${months[dateArray[1] - 1]} ${dateArray[0]}`; } return value; } }

*ngIf

答案 2 :(得分:1)

您的代码中基本上发生的是,当代码第一次运行时,变量varible没有赋值。因为它会返回并显示错误val content = latestAudioFile?.readBytes() val inputStream = this.getAssets().open("XXXXX-6e000f81XXXX.json") val credentials = GoogleCredentials.fromStream(inputStream) .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform")) val credentialsProvider = FixedCredentialsProvider.create(credentials) val speechSettings = SpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider) .build(); SpeechClient.create(speechSettings).use { speechClient -> val recognitionAudio = RecognitionAudio.newBuilder().setContent(ByteString.copyFrom(content)) .build() val speakerDiarizationConfig = SpeakerDiarizationConfig.newBuilder() .setEnableSpeakerDiarization(true) .setMinSpeakerCount(1) .setMaxSpeakerCount(4) .build() val config = RecognitionConfig.newBuilder() .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) .setLanguageCode("en-US") .setSampleRateHertz(8000) .setDiarizationConfig(speakerDiarizationConfig) .build() val recognizeResponse = speechClient.recognize(config, recognitionAudio); val alternative = recognizeResponse.getResults( 0 /*recognizeResponse.getResultsCount() - 1*/ ).getAlternatives(0) var wordInfo = alternative.getWords(0) var currentSpeakerTag = wordInfo.getSpeakerTag() val speakerWords = StringBuilder( String.format( "Speaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord() ) ) for (i in 1 until alternative.getWordsCount()) { wordInfo = alternative.getWords(i) if (currentSpeakerTag == wordInfo.getSpeakerTag()) { speakerWords.append(" ") speakerWords.append(wordInfo.getWord()) } else { speakerWords.append( String.format( "\nSpeaker %d: %s", wordInfo.getSpeakerTag(), wordInfo.getWord() ) ) currentSpeakerTag = wordInfo.getSpeakerTag() } } ,因此需要验证管道内的值是否具有值。请在下面找到示例代码:

Cannot read property 'split' of null