question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tostr messages not working. Script error in browser console.

See original GitHub issue

Hi,

I followed the instructions to setup toastr as per the readme. Am using Angular version 4.1.3. ngx-toastr version is 5.2.4

this.toastrService.success(‘Hello world!’, ‘Toastr fun!’);

The above line is throwing the following exception in browser console:

dom-portal-host.js:45 Uncaught TypeError: Cannot read property 'attachView' of undefined at DomPortalHost.attachComponentPortal (dom-portal-host.js:45) at DomPortalHost.BasePortalHost.attach (portal.js:47) at OverlayRef.attach (overlay-ref.js:10) at ToastrService._buildNotification (toastr-service.js:152) at ToastrService.success (toastr-service.js:25)

Any help is greatly appreciated.

Thanks, Hari

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
trevor-hackettcommented, Jun 17, 2017

I’ve implemented something like this before. Here is what I’ve done:

app.error-handler.ts:

import { ErrorHandler, Inject, Injector, NgZone, isDevMode } from "@angular/core";
import { Response } from "@angular/http";
import { ToastrService } from "ngx-toastr";

import * as Raven from 'raven-js'

export class AppErrorHandler implements ErrorHandler {
    constructor(@Inject(NgZone) private ngZone: NgZone, @Inject(Injector) private injector: Injector) { }

    private get toastr(): ToastrService {
        return this.injector.get(ToastrService);
    }

    public handleError(error: any): void {
        this.ngZone.run(() => {
            let errorTitle = 'Error';
            let errMsg = 'An unexpected error ocurred';

            if (error instanceof Response) {
                const contentType = error.headers.get("Content-Type")

                if (contentType && contentType == "application/json") {
                    const body = error.json();
                    errorTitle = body.message || errorTitle;
                    errMsg = body.detailedMessage || JSON.stringify(body);
                } else {
                    errMsg = error.text();
                }
            }
            this.toastr.error(errMsg, errorTitle);
        });

        if (!isDevMode())
            Raven.captureException(error.originalError || error);
        else
            throw error;
    }
}

And then in app.module.ts:

@NgModule({
  // ... other imports, declarations, etc.
  providers: [
    { provide: ErrorHandler, useClass: AppErrorHandler },
    // ... other providers
  ]
})
export class AppModule {}

This is very similar to what you’re doing. I’m not sure if NgZone is needed or not, it was working for HTTP error without it, but I decided to add it anyways, just in case.

1reaction
Silva-Macanetacommented, Jun 13, 2018

@yarrgh wow… thank you so much, i was losing hope.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Toastr notification not appearing when called - Stack Overflow
I tried the following code but nothing's popping up when I launch the page in my browser $(function ...
Read more >
Error.prototype.toString() - JavaScript - MDN Web Docs - Mozilla
The toString() method returns a string representing the specified Error object. ... error"); e4.name = ""; e4.message = undefined; console.log(e4.
Read more >
JavaScript console.error() Method - W3Schools
The error() method writes an error message to the console. The console is useful for testing purposes. Syntax. console.error(message). Parameters ...
Read more >
toast options not working - Material Design for Bootstrap
p>Hi I am trying to use TOAST message to display error message. ... <script> Command: toastr["error"]("Incorrect Username or Password!
Read more >
Click() method not working and Console returns an error ...
getElementsByClassName(…).click is not a function” error created while using click() method in JavaScript. The TypeError is an object that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found