Issues encountered when trying to integrate RXJS operators with Firebase

Currently, I am utilizing RXJS operators in combination with the firebase observable (specifically distinct and filter). If you would like to see my firebase tree, you can access it here. Below is an excerpt of my code:

let places this.db.list(`users/${this.authProvider.getUID()}/visitedPlaces`,{
    query:{
      orderByChild:"googleId"
    }
  });
places.distinct((p)=>{ 
  console.log(p)
  return p.googleId;
}).
   subscribe((snap)=>{
      console.log(JSON.stringify(snap,null,2))
    },(err)=>{
      console.log(JSON.stringify(err,null,2))
    },()=>{
      console.log("completed");
    });

I am attempting to filter the firebase data based on googleId. Although no errors are being generated, the distinct list is not functioning as expected.

If anyone has any insights or suggestions for why this may be happening, I would greatly appreciate it. Thank you.

Answer №1

It seems like the issue lies in using the distinct operator on an array instead of a stream of objects. This results in p being treated as an array within the distinct operator, rather than the individual objects inside the array. To resolve this, you can achieve the desired outcome with plain JavaScript using a map function:

Rx.Observable.of([
  { id: 1},
  { id: 2},
  { id: 1},
  { id: 3},
  { id: 2}
]).map(x => x.reduce((a, c) => {
   if (!a.some(y => y.id === c.id)) {
      a.push(c);
   }
   return a;
  }, [])
).subscribe(x => { console.log(x); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx.js"></script>

Alternatively, you can expand the array using the flatMap method like this:

Rx.Observable.of([
  { id: 1},
  { id: 2},
  { id: 1},
  { id: 3},
  { id: 2}
])
.flatMap(x => x)
.distinct(x => x.id)
.subscribe(x => { console.log(x); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging Runtime Environment Variables in Angular 5 within the app.module.ts File

I am currently utilizing a third-party authentication module called OktaAuthModule. In order to import it into my root module (app.module.ts), I must first configure it as follows: const config = { url: https://myurl.com/ } @NgModule({ declaration ...

Utilizing several root applications for the login component

I am in need of creating a login page that users are automatically directed to without any layout. Once they successfully log in, I would like them to be redirected to the main application. What is the most common approach for achieving this? Do I need m ...

Utilizing the arr.push() method to replace an existing value within an array with a new object, rather than simply adding a new

Seeking help to dynamically render a list of components that should expand or shrink based on values being added or removed from an array of custom objects. However, facing an issue where pushing a value into the array only replaces the previous value inst ...

An error occurred involving the absence of a static method called registerDefaultInstance within Firebase Performance and Espresso Instrumented Tests

Whenever I add implementation 'com.google.firebase:firebase-perf-ktx:19.1.0', my Espresso instrumented tests fail to start ("app" scheme building is fine). When attempting to run an Espresso test, the error message displayed is: Test running fail ...

Adding a firebase-messaging-sw.js file to Bubble? [UPDATE - file not compatible]

While troubleshooting an error message, I came across a common resolution that suggests adding a firebase-messaging-sw.js file. However, since I am using a bubble HTML element to run the script, I am unsure about the proper way to do this. I attempted usin ...

Setting an Observable reference to null within a map operator does not have any impact

I am currently working on developing a generic DataService that includes hateoas implementation. Within this setup, there is a REST API endpoint called /root which provides all the required hateoas links. For instance, { _links : { login : { ...

Definitions for images in the following format

I am currently utilizing typescript in conjunction with NextJs and next-images. Here is the code snippet: import css from "./style.sass"; import img from './logo.svg'; import Link from 'next/link'; export default () => <Link hre ...

CORS policy is preventing a patch request in loopback 4 from going through

Whenever I attempt to send a patch request to my API using Loopback 4, I encounter an error. All other requests work perfectly fine except for the patch request. The error message states: Access to XMLHttpRequest at 'api url' from origin ' ...

Is it advisable to standardize or de-standardize tree-structured data while creating the ngrx state?

In the process of developing a todo application, I have encountered a challenge regarding the structure of my model. Initially, before implementing ngrx, my Todo model included properties for sub Todos like this: export interface Todo { id: string; ti ...

What is the best way to define this.someProperty in a React component using TypeScript?

I'm encountering an issue with TS2339: Property 'someProperty' does not exist on type ''. I am attempting to add a new property to my React component using this.someProperty. interface MyComponentState { allClear: boo ...

Even after setting a new value to a variable in Vue, it may still reference the old

init(){ this.unsortedList = this.selectedVoucher.approvalStepList; // list in original order this.sortedList = this.unsortedList .sort(function(a,b){ if (new Date(a.createDate) < new Date(b.createDate)) return -1; ...

Retrieve the user's name with the highest points using Android Studio's Firestore

In order to display the person with the highest number of points at the top of a scoreboard, I crafted this query: Query query = collectionRef.orderBy("points", Query.Direction.DESCENDING).limit(1); Unfortunately, this query only produces another query a ...

Splitting Angular 4 code using angular-cli

My project is being built using angular-cli (ng build --prod), but I am encountering three issues in my production build: The rendering blocking style-sheet is 74 kb The vendor.bundle.js is extremely large at 1.1 MB The main.bundle.js is also quite large ...

Utilizing Angular 2 directives through an npm package

Wanting to share a directive on npm, I followed the steps in this documentation: Copied the compiled .js file from the .ts file (did not copy the map file) Created a new folder on my desktop and pasted it there Ran npm init and npm publish Started a new ...

Guide on organizing the Object into a precise structure in Angular

I am looking to transform the current API response object into a more structured format. Current Output let temp = [ { "imagePath": "", "imageDescription": [ { "language": "en ...

How to connect a form component object with a parent object in Vue3 using v-model and the Options API?

My inquiry is quite straightforward. I am exploring the official Vue documentation that delves into v-model arguments in relation to forms within a child component. Here is the code snippet I am referring to: (App.Vue) <script> import UserName from & ...

Investigating the Angular signals that triggered the effect

Is there a way to determine which Angular signal triggered an effect? I have two signals and one effect in a component. One signal is used to start a timer function within a service, while the other signal reacts to changing conditions in the timer functio ...

Setting up the propTypes for interface in React TypeScript

How can I specify the correct PropTypes for a property that is an interface in TypeScript with PropTypes? Requirements - Implementing both TS and PropTypes. Goal - To have a more precise type definition than PropTypes.any that meets standard eslint an ...

Discover the step-by-step guide to setting up forwarding in React Router 5

Just diving into the world of React and TypeScript. I'm working with a component called Err. Is there a way to redirect it using React Router 5? import React, { FC, Fragment, useEffect } from "react"; const Err: FC<{ error: string }> = ({ erro ...

Struggling to determine data type in Typescript

My goal is to create an interface for my realm Database using TypeScript. Essentially, I have an automation bot and I want to monitor and track how users are utilizing it. To achieve this, I have designed specific schemas that will be integrated into an i ...