At my witts end with Cannot read property 'get' of undefined


#1

I have tried so many things and I can’t get ionic storage to work inside app.module.ts
It’s failing on tokenGetter: (() => storage.get('token'))
Hoping someone can help. I am a n00b with Ionic/Angular but not programming.

global packages:

    @ionic/cli-utils : 1.5.0
    Cordova CLI      : 7.0.1
    Ionic CLI        : 3.5.0

local packages:

    @ionic/app-scripts              : 2.0.0
    @ionic/cli-plugin-cordova       : 1.4.1
    @ionic/cli-plugin-ionic-angular : 1.3.2
    Cordova Platforms               : android 6.2.3 browser 4.1.0
    Ionic Framework                 : ionic-angular 3.5.0

System:

    Node       : v8.1.3
    OS         : Windows 10
    Xcode      : not installed
    ios-deploy : not installed
    ios-sim    : not installed
    npm        : 5.0.3
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { S101App } from './app.component';
import { AuthService } from '../providers/auth-service/auth-service';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { LoginPage } from '../pages/login/login';
import { RegisterPage } from '../pages/register/register';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { IonicStorageModule } from '@ionic/storage';
import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { Http, HttpModule } from '@angular/http';
// import { Storage } from '@ionic/storage';
import { Device } from '@ionic-native/device';

import { SchedulePage } from "../pages/schedule/schedule";


export function getAuthHttp(http, storage) {
  return new AuthHttp(new AuthConfig({
    noJwtError: true,
    noTokenScheme: true,
    globalHeaders: [{ 'Content-Type': 'application/json'}],
    tokenGetter: (() => storage.get('token')),  //"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjMzNTgsImV4cCI6MTUwMDY2NTA4MSwiZGlkIjoiQTY3NDgwODEtMDg5OC1BMDU0LTlGMzMxODM1RTEzODY0RTgifQ.nMEawzzQtwqmJSxxsA8k0G9FZL9ZSqoLcCwsUEi41U8"),
  }), http);
}

// 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjMzNTgsImV4cCI6MTUwMDAyNTExOSwiZGlkIjoiNURGRjdDMTYtRDREOC0xMDEyLTQxNTk4NjVGRDY5RjZBMzEifQ.FWe-jLIG9ryINpHENNwI8ynV-YRtb8tk_M_8VZHJouQ'
@NgModule({
  declarations: [
    S101App,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    LoginPage,
    RegisterPage,
    SchedulePage
  ],
  imports: [
    IonicModule.forRoot(S101App),
    IonicStorageModule.forRoot(),
    BrowserModule,
    HttpModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    S101App,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    LoginPage,
    RegisterPage,
    SchedulePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    Device,
    { provide: ErrorHandler, useClass: IonicErrorHandler },
    AuthService,
    {
      provide: AuthHttp,
      useFactory: getAuthHttp,
      deps: [Http]
    }
  ]
})
export class AppModule { }


#2
  1. Josh Morony’s book (and blog) are probably where you should start. Live there for a week.

  2. Don’t put functions in app.module.ts.

  3. Read the official Angular style guide.

  4. Your setup of Ionic looks fine. But you need a lot of background on where things should go, and what the purpose of different files is.


#3

I got it from another learning place.


#4

There are a lot of blog posts about Ionic that are out of date or just not very good. So be careful. If you copypasted most of the code, then the code source probably isn’t one to trust. The code you posted is not well styled. For one thing, it acts as though TypeScript does not exist, and TypeScript is a key aspect of the Ionic development environment.


#5

Why would they give that example here?


#6

As @AaronSterling mentioned a lot of examples are out of date, and that looks like an example of one to me.