No providers for CredentialService

Hello all.

First of all thanks a lot for this sdk. To be honest I never heard about it until 2 or 3 weeks ago. I don’t know lot of things about angular 2 but I was able to do interesting stuff really fastly(including PouchDB integration etc).
I found lot of subject discussions about this error of no providers for XXXXXX
However non of the case I looked at correspond to my case. And after hours I start to be a bit pist of about myself :slight_smile:

I am working with ionic 2.1.0

So the id is to do a HomePage (blank) which init a CredentialService and go to search in pouchDB if credentials are already there. If yes the user is directly redirected to user page. If not he is redirected to connection page. Everything is working when the credentials are already there. However when credentials are not there anymore (I used chrome console to clean the websql) I am well redirected to Connection page which is also using CredentialService and there PATATRA (like we say in french) I get Error

No providers for CredentialService!

So I tried to have a look to what happened but nothing…

Here is my app.component.ts:

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from 'ionic-native';

import { HomePage } from '../pages/home/home';
import {AuthService} from './utils/authservice';
import {MyProfileServiceDB} from './utils/db/myProfileServiceDB';
import {MyProfileService} from './utils/myProfileService';
import {FriendsProfileServiceDB} from './utils/db/friendsProfileServiceDB';
import {FriendsProfileService} from './utils/friendsProfileService'
import {UserCredentialServiceDB} from './utils/db/userCredentialServiceDB';
import {CredentialService} from './utils/credentialService'
import {HeaderBuilder} from './utils/headerBuilderService'

@Component({
  template: `<ion-nav [root]="rootPage"></ion-nav>`,
  providers: [AuthService, MyProfileServiceDB, HeaderBuilder, UserCredentialServiceDB, MyProfileService, CredentialService, FriendsProfileService]
})
export class MyApp {
  rootPage = HomePage;

  constructor(platform: Platform) {
	platform.ready().then(() => {
	  // Okay, so the platform is ready and our plugins are available.
	  // Here you can do any higher level native things you might need.
	  StatusBar.styleDefault();
	});
  }
}

So as we can see I declared my CredentialService

Now my Home.ts (I am using a authService to login to server). For the moment I did not plugged it but it is working

import { Component, NgZone } from '@angular/core';
import {AuthService} from '../../app/utils/authservice';
import { Main } from '../prog/main/tabs/main';
import { Connection } from '../connection/connection';
import {CredentSignup} from '../signups/credent/credentSignup';
import { NavController } from 'ionic-angular';
import { CredentialService } from '../../app/utils/credentialService';
import { MyProfileService } from '../../app/utils/myProfileService';

@Component({
  selector: 'page-home',
  template: `
  <ion-content>
  </ion-content>
  `
})
export class HomePage {

  authService : AuthService;
  nav : NavController;
  zone : NgZone;
  userCredentialService : CredentialService;
  usercreds : Object;
  myProfileService : MyProfileService;

  static get parameters() {
	return [[AuthService],[NavController], [CredentialService], [NgZone], [MyProfileService]];
  }

  constructor(authservice, navcontroller, userCredential, zone, myProfileService) {
	this.authService = authservice;
	this.nav = navcontroller;
	this.userCredentialService = userCredential;
	this.zone = zone;
	this.usercreds = {
	  email: '',
	  password: ''
	}
	this.myProfileService = myProfileService;
  }

  ionViewDidLoad() {
	this.userCredentialService.initCredentials().then( success => {
	  if(success){
		var creds = this.userCredentialService.getMyCredentials();
		this.zone.run(() => {
		  this.login(creds);
		});
	  } else{
		this.nav.setRoot(Connection);
	  }
	}
  )
}

login(usercreds) {
  // wait profile is initialized then redirect on MainPage
  this.myProfileService.initMyProfile().then( ready => {
	if(ready){
	  this.nav.setRoot(Main);
	}
  })
}
}

And to finish my Connection page:

import { Component, NgZone } from '@angular/core';
import {AuthService} from '../../app/utils/authservice';
import { Main } from '../prog/main/tabs/main';
import {CredentSignup} from '../signups/credent/credentSignup';
import { NavController } from 'ionic-angular';
import { CredentialService } from '../../app/utils/CredentialService';
import { MyProfileService } from '../../app/utils/myProfileService';

@Component({
  selector: 'page-home',
  templateUrl: 'connection.html'
})
export class Connection {

  authService : AuthService;
  nav : NavController;
  userCredentialService : CredentialService;
  usercreds : Object;

  static get parameters() {
	return [[AuthService],[NavController], [CredentialService]];
  }

  constructor(authservice, navcontroller, userCredential) {
	this.authService = authservice;
	this.nav = navcontroller;
	this.userCredentialService = userCredential;
	this.usercreds = {
	  email: '',
	  password: ''
	}
  }

  ionViewDidLoad() {
  }

  login(usercreds) {
	this.authService.login(usercreds).then(data => {
	  if(data){
		// store the credentials for the next connection only if there were not already there
		this.userCredentialService.push(usercreds.email, usercreds.password, data);      
		this.nav.setRoot(Main);
	  }
	})
  }
  register(){
	this.nav.push(CredentSignup);
  }

}

Do you see something wrong??

I declared my service at Root level to be shared by all application but nothing. Ah by the way I tried as well to put it in the app.module.ts but not working as well

I really need to have this service shared by all the component. That’s why I want to cry now ! T_T

Does someone has an idea?

Thanks a lot ionic community!

Regars

Geoffrey

You have to import it in app.module.ts

And also remove providers line from app.component.ts.

Do I have to import it as providers or in the imports section

@NgModule({
  declarations: [
	MyApp,
	HomePage,
	CredentSignup,
	ProfileSignup,
	Main,
	FriendsTab,
	MenuTab,
	MyTab,
	NotificationTab,
	Profile,
	Connection
  ],
  imports: [
	IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
	MyApp,
	HomePage,
	CredentSignup,
	ProfileSignup,
	Main,
	FriendsTab,
	MenuTab,
	MyTab,
	NotificationTab,
	Profile,
	Connection
  ],
  providers: [AuthService, MyProfileServiceDB, HeaderBuilder, UserCredentialServiceDB, MyProfileService, CredentialService, FriendsProfileService]
})
export class AppModule {}

or like that:

@NgModule({
  declarations: [
	MyApp,
	HomePage,
	CredentSignup,
	ProfileSignup,
	Main,
	FriendsTab,
	MenuTab,
	MyTab,
	NotificationTab,
	Profile,
	Connection
  ],
  imports: [
	IonicModule.forRoot(MyApp), AuthService, MyProfileServiceDB, HeaderBuilder, UserCredentialServiceDB, MyProfileService, CredentialService, FriendsProfileService
  ],
  bootstrap: [IonicApp],
  entryComponents: [
	MyApp,
	HomePage,
	CredentSignup,
	ProfileSignup,
	Main,
	FriendsTab,
	MenuTab,
	MyTab,
	NotificationTab,
	Profile,
	Connection
  ],
  providers: []
})
export class AppModule {}

In first cas the same issue appears

In second I get Uncaught Error: Unexpected value ‘AuthService’ imported by the module ‘AppModule’

(Ah btw I removed them from the app.component.ts)

What is strange is that it is the only service which cause issue while I followed the same process. Make it Injectable declare it as providers at the highest level and then reuse it

Thanks for your help :slight_smile:

Found!!! The issue is in my connection page:

import { CredentialService } from '../../app/utils/CredentialService';

It must be:

import { CredentialService } from '../../app/utils/credentialService';

Thanks a lot anyway. I put what you said to have my app clearer

Best regards

Geoffrey MUSELLI

Nice :slight_smile:
And btw, the first solution is the best in your last message.