0

I have a method that returns an array of objects

I need to filter it by a certain criterion, and then create a new array of objects, where the data will be converted to another model

and then return the resulting array, then subscribe to it

public getTestWithPare(): Observable<Array<TestModel>> {

    return this.http.getTests().pipe(
        flatMap(rate => rate), // splitting array
        filter(test => !test.isDone),
        map(test => {

            const testPare = new TestModel();
            testPare.key = `${test.id}/${test.name}`;
            if (test.type === 'new') {
                testPare.finish = test.value;
                testPare.show = true;
            } else {
                testPare.start = test.value;
                testPare.hide = true;
            }

            return testPare;
        }),
        concatAll(),
        toArray()
    );
}

after calling the method, I get an error:

You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

I can not understand what is wrong

I want to get an array of objects at the output

approximately in this format:['key': {key: '1 / t', finish: 7, show: true}, ...]

cat1244
  • 69
  • 7
  • you may refer answers to the similar questions here - https://stackoverflow.com/questions/43549223/typeerror-you-provided-an-invalid-object-where-a-stream-was-expected-you-can-p https://stackoverflow.com/questions/44633266/you-provided-an-invalid-object-where-a-stream-was-expected-you-can-provide-an-o?rq=1 – Tushar Walzade Dec 18 '18 at 07:33
  • I watched, but this is not really my case I wanted to integrate the Observable into an array maybe i'm not doing it right – cat1244 Dec 18 '18 at 07:39
  • What about simply removing `concatAll`? `toArray` should be enough, if I understand correctly your case – Picci Dec 18 '18 at 07:47
  • it will return an array of models [TestModel, TestModel, ...] And working with them is not very convenient further. – cat1244 Dec 18 '18 at 07:59
  • because I would like to return the array of the format ['key': {key: '1 / t', finish: 7, show: true}, ...]. so that I can take html from this object by key values – cat1244 Dec 18 '18 at 08:01
  • `['key': {key: '1 / t', finish: 7, show: true}, ...]` does not exist, you can only do this : `{'key': {key: '1 / t', finish: 7, show: true}, ...}`. In order to help you could you create stackblitz with dummy getTests() observable ? – Yanis-git Dec 18 '18 at 08:06
  • All you need is the map method ... take the incoming array, filter/transform it and return it. Rxjs is not lodash. – Davy Dec 18 '18 at 08:09
  • since an array comes in, you’ll probably get something like map (tests => {tests.filter (test =>! test.isDone) .map (test => {...})}). I think it is not very beautiful and there is some more normal way – cat1244 Dec 18 '18 at 08:17
  • What is `TestModel `? It's probably not an Observable so `concatAll` throws an error trying to subscribe to it. – martin Dec 18 '18 at 11:01

1 Answers1

1

Rxjs is overkill here (only if you have big arrays and you don't want to handle it in 1 go), user array methods

const testToTestPare = test => {
  /* If you don't need TestMode, create an empty object const testPare = {} */
  const testPare = new TestModel(); 
  testPare.key = `${test.id}/${test.name}`;
  if (test.type === 'new') {
    testPare.finish = test.value;
    testPare.show = true;
  } else {
    testPare.start = test.value;
    testPare.hide = true;
  }
  return testPare;
};


public getTestWithPare(): Observable<Array<TestModel>> {
    return this.http.getTests().pipe(
        map(tests => tests.map(testToTestPare)),
    );
}