我的Angular ngrx / store效果没有被触发

时间:2018-10-12 21:48:13

标签: angular ngrx-store

  1. 我有一家专卖店
  2. 我已经派遣了该行动,但是效果并未被解雇。
  3. 这是什么引起的?

2 个答案:

答案 0 :(得分:1)

效果完成后将被禁用,就像Observable完成后将不会再触发一样。

导致效果意外完成的主要原因是未处理的错误。导致未处理错误的最常见原因是无法处理非2xx(例如非200)的HTTP响应。

以下是应如何执行的示例:

package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (otherArgs.length < 2) {
            System.err.println("Usage: wordcount <in> [<in>...] <out>");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        for (int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job,
                new Path(otherArgs[otherArgs.length - 1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }
}

在上面的代码中,如果HTTP请求由于某种原因失败,则会处理该错误并且不会禁用mergeMap(value => { // essential to catchError else an HTTP error response will disable this effect return this.myService.makeHttpRequest(value).pipe( catchError(() => { return of({}) // in the event of an error the value {} is passed down the pipe }) ) })

答案 1 :(得分:0)

效果总是采取一种行动,然后对减速器采取另一种行动。尝试向您的catchError部分添加新操作。

  @Effect()
  getToDos$: Observable<Action> = this.actions$.pipe(
    ofType(ToDoActionTypes.GetAllToDo),
    switchMap(() =>
      this.api.getTodos().pipe(
        map(todos => new GetAllToDoSuccess(todos)),
        catchError(err => of(new ErrorToDo(err)))
      )
    )
  );