Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshoot ERROR Error : "[object Object]" in angular

I am very new to Angular (version 6.1.2) so please bear with me. I am refurbishing the ngSpotify app that you can find in youtube but I am stuck with this error in the browser console.

ERROR Error: "[object Object]"
resolvePromisehttp://localhost:4200/polyfills.js:3136:31resolvePromisehttp://localhost:4200/polyfills.js:3093:17scheduleResolveOrRejecthttp://localhost:4200/polyfills.js:3195:17invokeTaskhttp://localhost:4200/polyfills.js:2743:17onInvokeTaskhttp://localhost:4200/vendor.js:34899:24invokeTaskhttp://localhost:4200/polyfills.js:2742:17runTaskhttp://localhost:4200/polyfills.js:2510:28drainMicroTaskQueuehttp://localhost:4200/polyfills.js:2917:25 core.js:1673
defaultErrorLogger
core.js:1673
./node_modules/@angular/core/fesm5/core.js/ErrorHandler.prototype.handleError
core.js:1719
next
core.js:4319:109
./node_modules/@angular/core/fesm5/core.js/EventEmitter.prototype.subscribe/schedulerFn<
core.js:3555:34
./node_modules/rxjs/_esm5/internal/Subscriber.js/SafeSubscriber.prototype.__tryOrUnsub
Subscriber.js:195
./node_modules/rxjs/_esm5/internal/Subscriber.js/SafeSubscriber.prototype.next
Subscriber.js:133
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype._next
Subscriber.js:77
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype.next
Subscriber.js:54
./node_modules/rxjs/_esm5/internal/Subject.js/Subject.prototype.next
Subject.js:47
./node_modules/@angular/core/fesm5/core.js/EventEmitter.prototype.emit
core.js:3539:52
onHandleError/<
core.js:3846:48
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke
zone.js:388
./node_modules/zone.js/dist/zone.js/</Zone.prototype.run
zone.js:138
./node_modules/@angular/core/fesm5/core.js/NgZone.prototype.runOutsideAngular
core.js:3783
onHandleError
core.js:3846
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.handleError
zone.js:392
./node_modules/zone.js/dist/zone.js/</Zone.prototype.runGuarded
zone.js:154
_loop_1
zone.js:677
./node_modules/zone.js/dist/zone.js/</</api.microtaskDrainDone
zone.js:686
drainMicroTaskQueue
zone.js:6

How to troubleshoot it? how to understand where it come from, and properly catch it? I tried the code for handleError in the Tour of Heroes tutorial of angular but without success. I found similar posts but that did not help me. Maybe I have to use Observable, or find where the error is thrown.

The code compiles. Here's my service:

import { Injectable } from '@angular/core';
/*
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http';
*/
import { map } from 'rxjs/operators';
import {Http, Response, Headers, RequestOptions} from '@angular/http';

import { Observable, of } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class SpotifyService {
  private searchUrl: string;

  constructor(private _http: Http) {

  }

  searchMusic(str: string, type = 'artist') {
    const accessToken = '<my token, which is probably bad to keep here>';
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization',  'Bearer ' + accessToken);
    let options = new RequestOptions({ headers: headers });
    this.searchUrl = 'https://api.spotify.com/v1/search?query=' + str + '&offset=0&limit=20&type=' + type + '&market=US';
    return this._http.get(this.searchUrl, options)
      .pipe(

    //EDIT: catchError(this.handleError('searchMusic', []),
    tap(res => console.log(this)),
    catchError((e) => this.handleError(e)),
    map(res => res.json())
      );
  }

   /*EDIT: private handleError(error: any): Promise<any> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
   }*/
   private handleError(error: any): Promise<any> {
     console.error('An error occurred', error); // for demo purposes only
   return Promise.reject(error.message || error);
   }

}

and the component calling it:

import {Component, OnInit} from '@angular/core';
import {SpotifyService} from '../spotify.service';

@Component({
  selector: 'search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {

  SearchStr: string;

  constructor(private _spotifyService:SpotifyService) { 
  }

  searchMusic() {
    this._spotifyService.searchMusic(this.SearchStr)
        .subscribe(res => {
          //EDIT: console.log(res.artists.items);
          console.log(res);
        });
  }

  ngOnInit() {  }

}
like image 569
aless80 Avatar asked Oct 29 '22 06:10

aless80


2 Answers

Your catchError is not properly defined, change it to this:

catchError((e) => this.handleError(e)),

Weird though, because even TypeScript should warn for sending wrong arguments to the handleError method.

It would be even better to keep using observables, instead of making a mix of promises and observables:

private handleError(error: any): Observable<any> {
  console.error('An error occurred', error); // for demo purposes only
  return of(error.message || error);
}
like image 92
Poul Kruijt Avatar answered Nov 12 '22 18:11

Poul Kruijt


I found the problem. I did not have the HttpModule module loaded. I added the following in app.module.ts:

import { HttpModule } from '@angular/http'; //NEW
..
@NgModule({
  imports: [
          ..
          HttpModule //NEW
like image 31
aless80 Avatar answered Nov 12 '22 18:11

aless80