I'm trying to connect an Ionic 2 app to Yelp's API. Right now I'm just using a blank Ionic 2 application generated from the CLI and running on my localhost.
My code:
app.module.ts
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule } from '#angular/core';
import { HttpModule } from '#angular/http';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
#NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
home.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '#angular/http';
import { Headers, RequestOptions } from '#angular/http';
import 'rxjs/add/operator/map';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
restaurants:any;
constructor(public navCtrl: NavController, public http: Http) {
var headers = new Headers();
headers.append('Authorization', 'Bearer someString');
var options = new RequestOptions({headers: headers});
this.http.get('https://api.yelp.com/v3/businesses/search?term=deli20&latitude=39.59754&longitude=-104.872934', options).map(res => res.json()).subscribe(data => {
this.restaurants = data.data.children;
console.log(this.restaurants);
});
}
}
home.html
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
The world is your oyster.
<p>
If you get lost, the docs will be your guide.
</p>
<!--Just using Ionic 2's default home page, just wanna output my JSON to the console for now.-->
</ion-content>
The page renders correctly, but the error I get in the console: "Failed to load resource: Preflight response is not successful"
The response looks like this:
{"error": {"code": "TOKEN_MISSING", "description": "An access token must be supplied in order to use this endpoint."}}
Any thoughts on what I might be missing?
Add this chrome plugin to your browser, turn it on and reload the page. It should work fine
Related
Error:
I am facing a problem when I run the code via ionic serve
I get an error of
Runtime error: Cannot find module "."
App.module.ts
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule } from '#angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { MyApp } from './app.component';
import { SigninPage } from '../pages/auth/signin/signin';
import { SignupPage } from '../pages/auth/signup/signup';
import { LandingPage } from '../pages/landing/landing';
import { AuthService } from '../services/auth.service';
#NgModule({
declarations: [
MyApp,
SigninPage,
SignupPage,
LandingPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
providers: [
StatusBar,
SplashScreen,
AuthService,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
I haven't mention the entrycomponents
and all the components contains the default code. AuthService is also empty
If I remove the AuthService form Providers in app.module file Code runs succesfully. But if I have to access the authService I must provide it in the Providers
IonicPageModule is missing from your project.
In your app.module.ts include
import { IonicPageModule } from 'ionic-angular';
And your import should looks like this:
imports: [
BrowserModule,
IonicPageModule.forChild(HomePage)
IonicModule.forRoot(MyApp)
],
after that run npm run-script build
NOTE: IonicPageModule is an NgModule that bootstraps a child IonicPage in order to set up routing.
I'm new to Ionic and experiencing the following error when I inject HttpClient into my service class:
Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[CategoriesServiceProvider -> HttpClient]:
StaticInjectorError(Platform: core)[CategoriesServiceProvider -> HttpClient]:
NullInjectorError: No provider for HttpClient!
Error: StaticInjectorError(AppModule)[CategoriesServiceProvider -> HttpClient]:
StaticInjectorError(Platform: core)[CategoriesServiceProvider -> HttpClient]:
NullInjectorError: No provider for HttpClient!
The error is triggered by the following Service class:
import { HttpClient } from '#angular/common/http';
import { Injectable } from '#angular/core';
#Injectable()
export class CategoriesServiceProvider {
apiURL = 'https://randomuser.me/api/?results=10';
constructor(private http: HttpClient) {
console.log('Hello CategoriesServiceProvider Provider');
}
}
If I remove the HttpClient from the constructor, the error goes away.
And, the following is my app.module.ts code:
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule } from '#angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { MyApp } from './app.component';
import { TabsPage } from '../pages/tabs/tabs';
import { CategoriesPage } from '../pages/categories/categories';
import { SubscriptionsPage } from '../pages/subscriptions/subscriptions';
import { CategoriesServiceProvider } from '../providers/categories-service/categories-service';
#NgModule({
declarations: [
MyApp,
TabsPage,
CategoriesPage,
SubscriptionsPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
TabsPage,
CategoriesPage,
SubscriptionsPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
CategoriesServiceProvider
]
})
export class AppModule {}
Any insights will be appreciated.
Add import { HttpClientModule } from '#angular/common/http'; in your app module.
In imports, add HttpClientModule like:
imports: [
BrowserModule,
HttpClientModule,
IonicModule.forRoot(MyApp)
]
I have this app I am building in Ionic and I tried adding navigation buttons from home page to both login and registration pages but when I add function (click)=”loginPage()” an error occured.
Below is the screenshot of the error:
App.module.ts
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule } from '#angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import { RegisterPage } from '../pages/register/register';
#NgModule({
declarations: [
MyApp,
HomePage,
LoginPage,
RegisterPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
LoginPage,
RegisterPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
home.ts
import { Component } from '#angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { LoginPage } from '../login/login';
import { RegisterPage } from '../register/register';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
loginPage(){
this.navCtrl.push(LoginPage);
}
registerPage(){
this.navCtrl.push(RegisterPage);
}
}
And home.html
<ion-content padding class="home">
<ion-grid>
<ion-row>
<ion-col col-12 class="div-two">Welcome</ion-col>
</ion-row>
<ion-row justify-content-start>
<ion-col col-6 class="col">
<div><button ion-button round (click)=”loginPage()”>Login</button></div>
</ion-col>
<ion-col col-6 class="col">
<div><button ion-button round (click)=”registerPage()”>Register</button></div>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
I have tried all I could but still don't know what the cause of the error is. i am new to ionic.
Thanks.
Your double quote charater is invalid. Do not use ”loginPage()” but use "loginPage()".
This sometimes happens when you copy some code to editor directly. Please be careful about that.
Why do I get this error?
Error: No component factory found for DatabaseProvider. Did you add it
to #NgModule.entryComponents?
I tried to add my provider to the entryComponents, but still not working.
database.ts
import { HttpClient } from '#angular/common/http';
import { Injectable } from '#angular/core';
import { Storage } from '#ionic/storage';
#Injectable()
export class DatabaseProvider {
localData: any;
constructor() {
console.log('Database provider iniciado ..');
}
}
My settings
, With this provider i'm trying to make a local storage, but I can't use this provider.
app.module.ts
import { NgModule, ErrorHandler } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { StatusBar } from '#ionic-native/status-bar';
import { SplashScreen } from '#ionic-native/splash-screen';
import { DatabaseProvider } from '../providers/database/database';
import { IonicStorageModule } from '#ionic/storage';
#NgModule({
declarations: [
MyApp,
InicioPage,
SimuladorPage,
NosotrosPage,
CuestionarioPage,
CuestinarioPorCompetenciaPage,
PersonalizadoPage,
CRUDPage,
NormasPage,
CategoriasPage,
VerCategoriasPage,
PreguntasPage,
AlternativasPage,
FeedbackPage
],
imports: [
BrowserModule,
IonicModule.forRoot(DatabaseProvider),
IonicStorageModule.forRoot()
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
InicioPage,
SimuladorPage,
NosotrosPage,
CuestionarioPage,
CuestinarioPorCompetenciaPage,
PersonalizadoPage,
CRUDPage,
NormasPage,
CategoriasPage,
VerCategoriasPage,
PreguntasPage,
AlternativasPage,
FeedbackPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
DatabaseProvider
]
})
export class AppModule {}
I just had to make a new project ....
I'm trying to build an ionic app to stream audio feeds.
Here's what I have so far...
app.module.ts
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule } from '#angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { StreamingMedia } from '#ionic-native/streaming-media';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
#NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
StreamingMedia,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
home.ts
import { Component } from '#angular/core';
import { NavController } from 'ionic-angular';
import { StreamingMedia, StreamingAudioOptions } from '#ionic-native/streaming-media';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, private streamingMedia: StreamingMedia) {
}
playStream() {
let options: StreamingAudioOptions = {
bgColor: 'red',
successCallback: () => { console.log('Audio played') },
errorCallback: (e) => { console.log('Error streaming') }
};
this.streamingMedia.playAudio('http://listen.radionomy.com:80/NewYorkClassicRock');
}
}
home.html
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
The world is your oyster.
<p>
If you get lost, the docs will be your guide.
</p>
<button ion-button large (click)="playStream()">Start the Stream!</button>
</ion-content>
Using ionic view, clicking the button does nothing in android or ios.
What am I doing wrong or missing?
Thanks!
It will not work in ionic view.
Run it on a physical device using the command:
cordova run android --debug
from within your app folder !
One small observation, you are setting the options but not passing it to the streamingMedia. You might want to do this:
this.streamingMedia.playAudio('http://listen.radionomy.com:80/NewYorkClassicRock',options);