'BehaivorSubject return null

I have this problem, I need the behaivorSubject not to return the value when it is null, since the result is grabbed by a component made by third parties and if this is null it throws me an error.

It should return the same only when I get the response from the server, I leave my code here:

  getCarList(params): Observable<CarListGrid> {
    if (this.carListSubject$ === undefined) {
      this.carListSubject$ = new BehaviorSubject<CarListGrid>(null);
      this.refetchCarList(params);
    }
    return this.carListSubject$.asObservable();
  }

  refetchCarList(gridParams) {
    const subscription = this.http.post<CarListGrid>(this.baseUrl + 'GetCar', {gridParams} ,httpOptions).pipe(map(data => this.setDefaultValuesToCar(data))).subscribe(
      (response) => {

        this.carListSubject$.next(response);
        subscription.unsubscribe();
      });
  }

Is there any way to prevent the subject from responding with a value when it is null? Or else, how can I give this code another way to help me with that?

Thanks!



Solution 1:[1]

You can use del to delete the one(s) you intended :

curl api | jq 'del(.items[] | select(.spec.taints[].effect == "NoSchedule")?)'

Solution 2:[2]

I hope this can be useful, in this example there are 3 items one with effect: NoSchedule, one with effect: anyother and one with taints empty. If am correct, you need only the second one:

{
  "kind": "NodeList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/nodes",
    "resourceVersion": "8768"
  },
  "items": [
    {
      "metadata": {
        "name": "ip-101-191-101-101.ec2.internal",
        "selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
        "uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
        "resourceVersion": "8768",
        "creationTimestamp": "2020-11-19T12:27:05Z"
          },
      "spec": {
        "podCIDR": "101.191.101.101/24",
        "providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
        "taints": [
          {
            "key": "worker-group",
            "value": "prometheus",
            "effect": "NoSchedule"
          }
        ]
      }
    },

  {
      "metadata": {
        "name": "ip-101-191-101-101.ec2.internal",
        "selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
        "uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
        "resourceVersion": "8768",
        "creationTimestamp": "2020-11-19T12:27:05Z"
          },
      "spec": {
        "podCIDR": "101.191.101.101/24",
        "providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
        "taints": [
          {
            "key": "worker-group",
            "value": "prometheus",
            "effect": "anyother"
          }
        ]
      }
    },
    {
      "metadata": {
        "name": "ip-101-191-101-101.ec2.internal",
        "selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
        "uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
        "resourceVersion": "8768",
        "creationTimestamp": "2020-11-19T12:27:05Z"
          },
      "spec": {
        "podCIDR": "101.191.101.101/24",
        "providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
        "taints": [

        ]
      }
    }


  ]

The command will be:

 jq '.items[]|select(.spec.taints[].effect!="NoSchedule") ' data1.jtxt

result:

{
  "metadata": {
    "name": "ip-101-191-101-101.ec2.internal",
    "selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
    "uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
    "resourceVersion": "8768",
    "creationTimestamp": "2020-11-19T12:27:05Z"
  },
  "spec": {
    "podCIDR": "101.191.101.101/24",
    "providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
    "taints": [
      {
        "key": "worker-group",
        "value": "prometheus",
        "effect": "anyother"
      }
    ]
  }
}

Note: I put your data ina a file to simulate input.

Solution 3:[3]

Based on the question, my understanding is that the taints could have multiple entries. It could be a mix of NoSchedule and other values. Here's the jq where the item will be ignored if at least one taint with effect value NoSchedule exists.

. as $in | $in * { items: $in.items | map(select(.spec.taints | all(.effect != "NoSchedule"))) }```

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Philippe
Solution 2 Alejandro Bermúdez
Solution 3 devang