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.

Impossible to initialize Moltin as a service in Angular 2

See original GitHub issue

Hi guys!

I’ve been trying to use the sdk with Angular 2 (actually the 4.1.3 version) initializing the Molltin package as a service, but without success.

The following code, is the typical used to start a new project in Angular 2 very simple, but it seems that the Moltin module is not returning the necessary things to register it as a service. Or maybe the problem is that I’m actually doing something wrong (probably xD)

Every time that I tried, the app throws an error with this message: Error: No provider for gateway!

So, a little bit of my code:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
// App modules
import { AppComponent } from './app.component';
// Moltin module
import { gateway as MoltinGateway } from '@moltin/sdk';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [
    MoltinGateway
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}```


Anybody with the same problem?

Many thanks in advance!

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
skaparatecommented, Jul 31, 2017

@gatoenano,

In your AppModule, You must provide an instance of the gateway with your client_id, otherwise the injector wouln’t find the required dependency :

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
// App modules
import { AppComponent } from './app.component';
// Moltin module
import { gateway as MoltinGateway } from '@moltin/sdk';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [
    { provide: MoltinGateway, useValue: new MoltinGateway({ client_id: '....' }) }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Edited: please, do not use this example. It may work, but is wrong.

1reaction
ynnojcommented, Aug 2, 2017

Going to close this. Please reopen if you’re still have issues @gatoenano.

Thanks for the assist, @skaparate! I’m going to document your recommended Angular approach in the Wiki 🙃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular2: Wait for service to initialize - Stack Overflow
I'm not using router (don't need it for the app), so I don't think @CanActivate is suitable in this case. Any ideas? @Component ......
Read more >
Add services - Angular
Services are a great way to share information among classes that don't know each other. Create a MessageService next and inject it in...
Read more >
Introduction to services and dependency injection - Angular
An injector creates dependencies and maintains a container of dependency instances that it reuses, if possible. A provider is an object that tells...
Read more >
Providing dependencies in modules - Angular
A provider is an instruction to the Dependency Injection system on how to obtain a value for a dependency. Most of the time,...
Read more >
Singleton services - Angular
For a sample application using the app-wide singleton service that this page ... Beginning with Angular 6.0, the preferred way to create a...
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