-1

I have made a component in the angular2+ file. I want to make an http request to the nodeJs server. However, i keep getting - >

Uncaught (in promise): TypeError: Cannot read property 'get' of undefined
TypeError: Cannot read property 'get' of undefined

undefined

To note, I am even unable to retrieve the value of the global variables stored in one function and (unable to) accessed in another function. And this is the first time i am facing it ever.

code -

app.component.ts

import { HttpClient, HttpParams, HttpRequest, HttpEvent } from '@angular/common/http';

@Injectable()
export class MachineMergerComponent implements OnInit {

    constructor(private http: HttpClient) {}

    onFileLoadTopFive(fileLoadedEvent) {

        // -----===================  Top Five Neovis  ==================== -------------

        this.rhsNameTopFive = [];
        this.relationshipInfo = [];

        for (var i = 0; i < 5; i++) {

            this.rhsNameTopFive.push(this.objectValueAfterConfidence1[i].rhs[0])

            console.log("this.rhsNameTopFive = ", this.rhsNameTopFive)

            // http request

            var relationObj = {
                'relationObj1': this.rhsNameTopFive[i]
            }

        }
        console.log("relationObj = ", relationObj);


        this.http.get("http://localhost:3003/seekRelationship?relationObj=" + this.rhsNameTopFive[0])
            .map(Response => Response)
            .catch((err) => {
                console.log("err =", err)
                return Observable.throw(err);

            })
            .subscribe((res: Response) => {

                console.log("XXXXXXXXXXXX Response on /seekExtraction", res);

                this.relationshipInfo[i] = res;

                console.log(" this.relationshipInfo[" + i + "]", this.relationshipInfo[i])
            })
    }

}

app.module.ts

import { HttpClientModule } from '@angular/common/http';

@NgModule({
  imports: [
    CommonModule,

    FormsModule,
    // HttpModule ,

    HttpClientModule,

  ],
  declarations: [   MachineMergerComponent,  ]
})
export class ApiSequenceModule { }
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
Techdive
  • 997
  • 3
  • 24
  • 49

2 Answers2

0

MachineMergerComponent is a service. add it under provider list. (also consider renaming it to MachineMergerService )

declarations: [  ... ],
providers:    [ MachineMergerComponent ]
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
  • Another option, depending on the Angular version, is to add `@Injectable({ providedIn: 'root' })`. Which makes it a singleton that does not have to be registered in the provider section. – Silvermind Feb 08 '19 at 09:58
  • @Sachila... i am getting error -> Component MachineMergerComponent is not part of any NgModule or the module has not been imported into your module. Error: Component MachineMergerComponent is not part of any NgModule or the module has not been imported into your module. – Techdive Feb 08 '19 at 10:08
0

try to initialize the data that you give to the libray don't affect NULL values.

taha-mou
  • 1
  • 1