TS2339 Error: The 'scan' property cannot be found on the 'Observable<Message[]>' type

I am currently following a guide on creating a chatbot using Angular. I encountered the following error:

ERROR in src/app/chat/chat-dialog/chat-dialog.component.ts(24,6): error TS2339: Property 'scan' does not exist on type 'Observable'.

The versions of Angular & Angular CLI that I am using are 6.0.3.

chat-dialog.component.ts

import { Component, OnInit } from '@angular/core';
import { ChatService, Message } from '../chat.service';
import { Observable } from 'rxjs';
import { scan } from 'rxjs/operators';

@Component({
  selector: 'chat-dialog',
  templateUrl: './chat-dialog.component.html',
  styleUrls: ['./chat-dialog.component.scss']
})
export class ChatDialogComponent implements OnInit {

  messages: Observable<Message[]>;
  formValue: string;

  constructor(public chat: ChatService) { }

  ngOnInit() {
    // appends to array after each new message is added to feedSource
    this.messages = this.chat.conversation.asObservable()
    .scan((acc, val) => acc + val);
  }

  sendMessage() {
    this.chat.converse(this.formValue);
    this.formValue = '';
  }

}

chat.service.ts

import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';

import { ApiAiClient } from 'api-ai-javascript';

import { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

// Message class for displaying messages in the component
export class Message {
  constructor(public content: string, public sentBy: string) {}
}

@Injectable()
export class ChatService {

  readonly token = environment.dialogflow.angularBot;
  readonly client = new ApiAiClient({ accessToken: this.token });

  conversation = new BehaviorSubject<Message[]>([]);

  constructor() {}

  // Sends and receives messages via DialogFlow
  converse(msg: string) {
    const userMessage = new Message(msg, 'user');
    this.update(userMessage);

    return this.client.textRequest(msg)
               .then(res => {
                  const speech = res.result.fulfillment.speech;
                  const botMessage = new Message(speech, 'bot');
                  this.update(botMessage);
               });
  }

  // Adds message to source
  update(msg: Message) {
    this.conversation.next([msg]);
  }

}

Answer №1

scan is an Operator that requires piping.

  The following code initializes the component:
    ngOnInit() {
    this.messages = this.chat.conversation.asObservable()
      .pipe(
        scan((acc, val) => acc + val)
      )
  }

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

Webpack encountering issues with loading dependencies within dependencies

When I try to run webpack, it seems that the compiler is having trouble loading my app.module from within main.ts. Without using webpack, my application can find all modules correctly, but with Webpack, it's failing. This is my webpack configuration: ...

Angular 2 rc1 does not support ComponentInstruction and CanActivate

In the process of developing my Angular 2 application with Typescript using angular 2 rc.1, I've noticed that the official Angular 2 documentation has not been updated yet. I had references to ComponentInstruction Interface and CanActivate decorator ...

Card collapse upon being clicked

I am encountering an issue with a collapsed card in Angular. Here is the code for my component: @Component({ selector: 'app-numbered-card', templateUrl: './numbered-card.component.html', styleUrls: ['./numbered-card.component ...

Utilizing a mat-list for showcasing a variety of items

In my TypeScript file, I have defined constant data like this: export const HELPERS: Helper[] = [ { id: '0', name: 'Joan', image: '/assets/images/img.png', designation: 'Chief', ab ...

I'm looking to locate the API documentation for AngularJS TypeScript

After transitioning from using AngularJS 1.4 and plain JavaScript to now working with AngularJS 1.5 but utilizing TypeScript, I have found it challenging to find helpful documentation. For instance, when trying to inject services like $q or $timeout into m ...

Dealing with request errors in Angular with RxJS - catchError() isn't as effective as expected

Update Alert: Problem Solved. Solution Provided Below In my service class, I have the following code snippet: public getListById(id:string): Observable<any[]> { return this.http.get<any[]>(`${BASE_URL.local}/${id}`).pipe( m ...

Having trouble importing React components from a different file?

Having trouble with React due to issues importing components from external files. import React from 'react'; import ReactDOM from 'react-dom'; import '../resources/styles.scss'; import WelcomeView from '../views/welcome/W ...

Unable to determine all parameters for Modal: (?, ?, ?)

import { Component, Inject } from '@angular/core'; import { NavController, Modal } from 'ionic-angular'; import { PopupPage } from '../../components/modal/modal.page'; @Component({ templateUrl: 'build/pages/spot/spot. ...

How to Handle CRUD Errors in NodeJS using Mongoose and Return a Custom Response to the Client

Setup NodeJS 10 MongoDB Client side app : Angular 9 About In my NodeJS application, I have a controller and service that work together to create an entity and return a promise. Here's how it looks: Controller async create(@Body() entityData: an ...

React Native TextInput utilizing FieldRenderProps

When working with final-form in React-Native, I encountered an issue while creating a custom TextInput. It seems that specifying the type for FieldRenderProps is resulting in an error: TS2769: No overload matches this call. Overload 1 of 2, '(props: T ...

Utilizing global enumerations within VueJS

Is there a way to effectively utilize global enums in Vue or declare them differently? My current setup is as follows: Within my types/auth.d.ts: export {}; declare global { enum MyEnum { some = "some", body = "body", o ...

How can I prevent further input in an input field after making a selection from mat autocomplete in angular?

Hello everyone, I am looking to disable the input field after selecting a value from the drop-down. Additionally, I want to be able to reset the selected value using a reset button. If you need a reference, you can check out Stackblitz: https://stackblitz ...

What are some strategies for distinguishing between callable and newable types?

I seem to be facing a challenge related to narrowing, specifically the differentiation between Fnc (callable) and Class (newable). The code snippet provided functions in Playground, but the autocomplete feature is lacking, as shown in the first image. My ...

Incorporate external JavaScript files into a cutting-edge Ionic 5 / Angular 9 project

I need assistance in incorporating jQuery functions into my Ionic 5/Angular 9 project. In order to access some of the functions, I have included the necessary files in the angular.json file and installed Jquery and Bootstrap through npm. However, I am un ...

What is the method to cancel an Observable subscription without having a reference to the object of type "Subscription"?

If I were to subscribe to an Observable without an object of type "Subscription," how can I properly unsubscribe from it? For instance, if my code looks something like this: this.subscription = bla ... I know I can easily unsubscribe using the following ...

The console is displaying an undefined error for _co.photo, but the code is functioning properly without any issues

I am facing an issue with an Angular component. When I create my component with a selector, it functions as expected: it executes the httpget and renders a photo with a title. However, I am receiving two errors in the console: ERROR TypeError: "_co.photo ...

Utilize Lodash to iterate through functions in a loop and retrieve the first matching result

I am looking to iterate through an array of objects and call a method on them. If the result of that method meets certain conditions, I want to immediately return that result. The current implementation is as follows: public getFirstMatch(value: string, a ...

Exploring the implementation of window.addEventListener within an Angular project

I am currently working on testing a method in Angular using Jasmine. However, I am running into an issue with triggering the mouse event (specifically when the browser back button is clicked). Below is the code snippet I'm working with: navigate() { ...

Can you determine the base class type based on the derived class?

I've encountered a typings issue while working on a new feature for my TypeScript library. Let's dive into the problem with the following example: class Base { ... } class User extends Base { ... } class Product extends Base { ... } class Comp ...

Consider the potential for receiving an undefined return when a default value is not specified

I developed a React hook that makes a request to a remote API and returns a value. By default, when the API is fetching, the returned value is set to undefined. However, I added a new option that allows for setting a default value - so if the API is still ...